I am having an issue when setting the RelativeLocation of my UCameraComponent.
UPROPERTY(VisibleDefaultsOnly, Category = Camera)
AFPSCharacter::AFPSCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
// Create the first person camera
FirstPersonCameraComponent = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
FirstPersonCameraComponent->AttachParent = GetCapsuleComponent();
// Position the camera a bit above the eyes
FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 40.0f + BaseEyeHeight);
// Allow the pawn to control rotation.
FirstPersonCameraComponent->bUsePawnControlRotation = true;
As you can see the location is still 0,0,0 and the camera is showing up below the character’s waist.
I have also tried using SetRelativePosition().
However, neither approaches are working.
It has worked a couple times, but for the most part it doesn’t. Which is leading me to think this might be a bug.
Not sure wahts going on here, maybe there something wrong attachment, considering capsule is root, try using RootComponent insted of GetCapsuleComponent().
But if you find don’t find fix for that, here work around. Insted of suing camera components, override CalcCamera function which sets camera position of view target (by default it pick camera component)
I tried using RootComponent and also GetRootComponent() but they both gave the same results as GetCapsuleComponent()
You can see from the hierarchy in my original screenshot that the FirstPersonCameraComponent is already attached to the CapsuleComponent correctly.
I also, checked and the mobility of FirstPersonCameraComponent is movable.
Most example code I can find is using this UCameraComponent method, so I’m very confused as to why this isn’t working
So, I ended up moving the RelativeLocation code out of the constructor and into a PostInitializeComponents() override.
virtual void PostInitializeComponents() override;
FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 20.0f + BaseEyeHeight);
I’m not sure if this is the best place for it, but it is at least working now.
I would really like to understand why it won’t work in the constructor? Because all of the sample code I’ve seen has it there.
Is this a bug?
The following also won’t work in the constructor and I had to also move it to PostInitializeComponents()