I want to create a system such that when the user presses a button, nearby objects will display a billboard featuring the name of the object and a sprite showing its category (defined by colours). I am lost on how to create the billboard.
I’m thinking that I could use a UTextRenderComponent for the text and a UBillboardComponent for the sprite, but I can’t figure out how to get the text to face the camera (main problem is that I can’t even seem to get the camera object), and the billboard component shows up in the editor but not the game.
Is there a better way to go about this?
Here is a preview how it will look
Pickup.h
UCLASS()
class APickup : public AActor
{
GENERATED_BODY()
public:
APickup();
public:
UPROPERTY(EditDefaultsOnly)
class USphereComponent *Collision;
UPROPERTY(EditDefaultsOnly)
class UMaterialBillboardComponent *Billboard;
UPROPERTY(EditDefaultsOnly)
class UTextRenderComponent *TextRender;
};
Pickup.cpp
#include "Components/MaterialBillboardComponent.h"
#include "Components/TextRenderComponent.h"
#include "Engine/Font.h"
#include "Materials/MaterialInterface.h"
APickup::APickup()
{
Collision = CreateDefaultSubobject<USphereComponent>(TEXT("Collision"));
Collision->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
Collision->SetCollisionResponseToAllChannels(ECR_Ignore);
Collision->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
RootComponent = Collision;
Billboard = CreateDefaultSubobject<UMaterialBillboardComponent>(TEXT("Billboard"));
Billboard->AttachTo(RootComponent);
TextRender = CreateDefaultSubobject<UTextRenderComponent>(TEXT("Text"));
TextRender->AttachTo(RootComponent);
TextRender->SetText(FText::FromString("Pickup Text"));
TextRender->HorizontalAlignment = EHorizTextAligment::EHTA_Center;
TextRender->VerticalAlignment = EVerticalTextAligment::EVRTA_TextCenter;
static ConstructorHelpers::FObjectFinder<UFont> FontClass(
TEXT("Font'/Game/FirstPerson/Blueprints/TestFont.TestFont'"));
if (FontClass.Object)
TextRender->SetFont(FontClass.Object);
static ConstructorHelpers::FObjectFinder<UMaterialInterface> Material(
TEXT("Blueprint'/Game/FirstPerson/Blueprints/M_Font.M_Font'"));
if (Material.Object)
TextRender->SetMaterial(0, Material.Object);
}
Font setup:
Create new font and make sure to make it a offline font
Create new material, Shading Model = Unlit, Blend Mode = Masked