How to change the Camera Rotation while in-game?

Hey guys,

I’m trying to do a lean effect to the right and also to the left. The effect is pretty simple, rotate the camera X axis, but since it’s a first person shooter i’m using the pawn control rotation to handle the mouse control.

Any ideas to make this effect happen?

Timers and updating the RelativeRotation on your camera component for your actor.

I’m assuming you’re making a lean around a corner under cover type situation.

What I would do is on the leanstart action, start a timer, and over the course of the timer update the RelativeRotation for your camera component. Then, when the timer hits a certain point, or the camera is rotated a certain amount, call for the timer to end or to restart and do the opposite on the second run.

I’ll cook up a code example in a few minutes.

This may have some errors, but it should get you the effect you need.

Just make sure you create the required variables that I just kinda threw in there. I tried to make the code as self explanatory as possible.



void YourClass::StartLean()
{
	float RepeatTime = 0.5f; // Set this to a good amount of time for a nice lean effect.
	GetWorldTimerManager().SetTimer(this, &YourClass::LeanTimerDelegate, RepeatTime, true);
}

void YourCLass::LeanTimerDelegate()
{
	// This repeats every time the timer ends (Or the RepeatTime above)
	if (CameraComponent->RelativeRotation >= MAX_ROTATION)
	{
		// End the timer and add no more lean
		GetWorldTimerManager().ClearTimer(this, &YourClass::LeanTimerDelegate);
		return;
	}
	
	// Otherwise, add the rotation amount that fits
	CameraComponent->RelativeRotation += FVector(50.0f,50.0f,50.0f); //Just plugin the correct values for your effect
}


Also note, this is only for one way lean, you’ll have to modify/duplicate the code to reverse or set other directions or whatnot.

Hey !
Your code works! But only for when the Pawn Rotation Control is deactivated, any idea how to do the effect with the Rotation Control active?

Here is the working code for you people




void AFPSCharacter::LeanRight()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("LeanRight()"));
	float RepeatTime = 0.01f;
	GetWorldTimerManager().SetTimer(Lean,this, &AFPSCharacter::LeanDelegate, RepeatTime, true);
}

void AFPSCharacter::LeanDelegate()
{
	if (Camera->RelativeRotation.Roll >= 30.0f)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("ClearTime"));
		GetWorldTimerManager().ClearTimer(Lean);
		return;
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Rotate"));
		Camera->RelativeRotation += FRotator(0, 0, 1.2f);
	}
}

// on .h
// FTimerHandle Lean


Awesome to hear it works… mostly.

Im assuming your FPS wont be using much of the camera’s Roll except when leaning. And typical camera/mouse movement is done on pitch/yaw. So you can actually tell it to disable control rotation for certain parts.

bUseControllerRotationPitch = true;
bUseControllerRotationYaw = true;
bUseControllerRotationRoll = false;

I would just disable the usage of the Roll until the lean is over. There is a way to change the roll with the rotation set, but it doesnt prevent the user from just overriding your changes. The method in question is SetControlRotation(); But, this lets the controller have the ability to modify it if the user inputs, or from a bug. So disabling the usage until the timer clears is a safer way to lock the lean in.

I don’t know why but I changed it to use the Controller rotation instead of the pawn control rotation and it works on the fly now.
The only major issue now is that the lean effect only works if i’m pressing any sort of movement key (WASD), lean is mapped as an action, but jump and crouch works in idle state, have any idea what’s going on?

Edit: Any mouse movement also triggers the lean effect