[Bug? C++] StopMovementImmediately causes camera stutter

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.

Hi ,

Would you be able to provide the full code (.h and .cpp files) for your character class? Are you making all of your customization in the code class, or do you make changes to your character Blueprint as well?

Hi ,

We have not heard back from you for a few days. Do you still need any help with this issue? I will be marking this post as resolved for internal tracking purposes, but please feel free to reopen this post at any time if you have any additional information or need any more help.

Hi .
Apologies for the extremely late reply. Just about all of my customization is in code. Is there an email address I can reach you at to email you the source?

Hi ,

If you need to contact me privately, you can [send me a PM][1] on the forums. You can’t attach files directly to a PM, but you can put the files in Dropbox or Google Drive and send a link to it.

Hi ,

I downloaded the files you provided, thank you for that. I created a new code project using the first person template and replaced all of the existing code with the files you provided. After recreating the original code you were using and moving the player start some ways into the air, I played the game in PIE mode and after landing did not see any jitter when moving the camera. I do have some additional questions, though.

  • What version of the Engine are you using? I was trying with version 4.7.5 built from source code.
  • From the Vine you linked to, it looks like the level itself is moving (or rotating). Is that correct?
  • Is my test case of moving the player start into the air and letting the character fall until it lands appropriate for this issue?
  • Do you see this jitter happen in PIE mode, or are you using Standalone or a packaged game?

Hi ,

I spent some time looking into this again today, and while I can see the vertical jittering in the project that you provided, I was unable to recreate it in a new project. I talked it over with someone else, and we think there may be some interaction with code in the Engine that is intended to prevent gimbal lock. However, we aren’t sure where this interaction is occurring.

I am currently attempting to build up a copy of your character class in a new project, one piece at a time, to see if I can reproduce the issue there. It may take a while for me to be able to complete that process, though.

Hi ,

I apologize for the delay in getting back to you on this issue. I was unable to reproduce the jittery camera movement in my own project, but I went ahead and entered a report about the issue to have it investigated further (UE-16321), and attached the project you provided where the issue occurs.

Hi ,

I apologize for how long it took us to get this issue figured out. We recently began digging into the source code for how the camera rotation is handled, but could not figure out what was going wrong with your project or how to reproduce the issue in a clean project. I just went back into the code you had included with your project, and I believe I found the problem. I can’t believe I missed it when I was looking initially.

In the function ASpaceGameCharacter::LookY(), the line AddToActorRotation(FRotator(-Value, 0, 0)); was never being run, so any change to the vertical rotation of the camera was not being applied until the AddToActorRotation() function was called due to horizontal rotation. If I moved that line to the very end of the LookY() function, or moved it up a few lines into the previous if() statement, the jittery vertical rotation disappeared. Would you be able to try moving that line to see if it helps get rid of the jerkiness you are seeing?