How to clamp Pitch after AddLocalRotation()?

SpringArm->AddLocalRotation({ static_cast(RotatePitchAxis * 30.0f * DeltaTime), 0.0f, 0.0f });

I am changing the local Pitch rotation, but afterwards I wish to make sure the pitch clamps between a minimum value and maximum value. I tried clamping the relative rotation as follows, but this messes up yaw and roll. I only want to make sure the Pitch stays between two values and not touch the yaw and roll.

auto Rotation = SpringArm->GetComponentRotation();
Rotation.Pitch = FMath::Clamp(Rotation.Pitch, SpringArmMinPitch, SpringArmMaxPitch);
SpringArm->SetRelativeRotation(Rotation);

Does anyone have a suggestions on clamping the pitch?

Hi wilricknl,

you’re messing things up because GetComponentRotation retrieves rotation in world space whereas SetRelativeRotation sets it in local space. Depending on whether you want to clamp your local or absolute pitch you’ll want to uniform your function calls – by either using GetRelativeRotation or SetWorldRotation.

hope that helps!

f

Thanks for the clear explanation. Replacing SpringArm->GetComponentRotation() with SpringArm->GetRelativeRotation() works as intented.