Initialize a static mesh component in C++ actor

Hello,

I try to create a custom class that inherits from ACharacter.
I want to add a static mesh to the capsule component, and put it below the character. So I try to change it’s location.

Here is the declaration :

UCLASS()
class ALivingCreature : public ACharacter
{
public:
    ALivingCreature(const FObjectInitializer& ObjectInitializer = FObjectInitializer::Get());
private:
    UPROPERTY(Category=Character, VisibleAnywhere, BlueprintReadOnly, meta=(AllowPrivateAccess = "true"))
	TObjectPtr<UStaticMeshComponent> FocusMesh;

And here is the constructor implementation :

ALivingCreature::ALivingCreature(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
    FocusMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FocusMesh"));
    FocusMesh->AlwaysLoadOnClient = true;
    FocusMesh->bOwnerNoSee = false;
    FocusMesh->bCastDynamicShadow = false;
    FocusMesh->bAffectDynamicIndirectLighting = false;
    FocusMesh->SetupAttachment(GetCapsuleComponent());
    static FName MeshCollisionProfileName(TEXT("NoCollision"));
    FocusMesh->SetCollisionProfileName(MeshCollisionProfileName);
    FocusMesh->SetGenerateOverlapEvents(false);
    FocusMesh->SetCanEverAffectNavigation(false);
    FocusMesh->SetRelativeLocation(FVector(0.0f, 0.0f, -90.0f));
    FocusMesh->SetComponentTickEnabled(false);
    FocusMesh->SetSimulatePhysics(false);
    FocusMesh->SetRelativeTransform(
        FTransform(
            FRotator(0, 0, 0),
            FVector(0.0f, 0.0f, -90.0f),
            FVector(1, 1, 1)
        )
    );
}

But it seems that the SetRelativeLocation and the SetRelativeTransform are not working (the location is still at (0, 0, 0) instead of (0, 0, -90) in the editor and in game.

What am I doing wrong ?

Thank you !

Apparently, it’s just that the liveCoding compiler doesn’t refresh everything in the Unreal Editor. I think my code was correct ^^

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.