Hi there.
Currently I’m having issues with GetMovementComponent()->StopMovementImmediately(); with the relative rotations of my camera in my game.
tl;dr, using GetMovementComponent()->StopMovementImmediately(); makes the camera’s relative pitch rotation stutter. Code and longer explanation below.
So I’m doing some development on having a character move around in an environment with non-standard gravity (centrifugal) by setting the world’s gravity to zero, some maths stuff, and some other orientation code, and while the player is moving around I’m using RelativeRotation for the camera’s pitch (so the player can see their body, control pitch doesn’t seem to be changeable for the this + orientation stuff). To get movement working well I’ve also had to set the character’s movement component to Flying to avoid having to mess around with altering the way walking movement works or writing movement stuff from scratch.
Now the flying physics builds up momentum as you move around, which I’ve offset with some checks to see if you’re moving or not, along with a bStopMovement toggle, then setting the character’s velocity to FVector(0,0,0) or using StopMovementImmediately() to avoid the player constantly drifting around. Unfortunately after calling this the relative rotation of my camera becomes extremely jittery, and I’m not entirely sure why.
Here’s a vine to illustrate Vine . As you can see this is now super jittery, and if I remove the code to stop the movement it works perfectly fine.
Camera pitch looking code:
void ASpaceGameCharacter::LookY(float Value)
{
Value = Value / 3;
FirstPersonCameraComponent->RelativeRotation.Pitch = FMath::ClampAngle(FirstPersonCameraComponent->RelativeRotation.Pitch - Value, -89, 89);
}
Stop movement code:
bHittingGround = bIsHittingGround();
if (GravityEnum != EGravityEnum::GE_None && bHittingGround)
{
if (bStopMovement)
{
if (!movingForward && !movingLeft)
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, "stopping movement");
}
GetCharacterMovement()->StopMovementImmediately();
bStopMovement = false;
//Stops the character from rocking back and forth and maintaining velocity
}
}
}
bStopMovement, and movingForward and movingLeft are set appropriate on key presses, and this works properly in getting the player to stop moving but has the drawback of messing up the camera’s rotation when you’re moving the mouse around.
However, I have somewhat found a way around this by setting the player’s velocity like: GetCharacterMovement()->Velocity = FVector(.1, .1, .1); and the relative rotations remain unaffected if this is used instead.
Perhaps this is a bug? Or is there someone here better versed in UE4 and C++ than I who could maybe explain why this is occuring? If any more code is necessary I am perfectly happy to provide it.
Thanks!
Edit: Also as stated above I managed to find a way around the issue and it works no problems now, but I thought it’d be better to post this up here, and try to figure out why it’s happening would probably be beneficial for all.