I had an issue in my game where I wanted to free look whilst driving a vehicle, but hated that I had to manually reset my camera to look straight forward. I made a solution which might help some people out!
First, you’ll need a way to know if the player is moving the camera. Maybe hook up a bool to your camera controls or something.
Next, attach the branch into a ‘Do Once’ node. This is to prevent the next part - the ‘Retriggerable Delay’ - from constantly firing.
The retriggerable delay can be any time. This is the time it takes from recieving no camera input to re-centering the camera. Attach that into the next part, a custom event called ‘Reset Camera’
The ‘Reset Camera’ event is pretty simple. We are using a ‘Timeline’ to lerp between the cameras current rotation, and target rotation. In most cases you’ll want the target rotation to be the rotation of the vehicle.
The Timeline is a simple 0-1 lerp over 1 second. Make sure it’s 1 second.
The Lerp (Rotator) is what we are using to actually move the camera - or in my case, the Spring Arm. Input A is the spring arm rotation and Input B is the vehicles rotation. Drag the alpha from the Timeline into the alpha slot of the Lerp (Rotator).
The branch is there to continuously check if the player has moved the camera. If true, the timeline will stop, allowing the player to continue to move the camera.
Plug the result of the Lerp into a Set World Rotation node, which controls the Spring Arm.
[OPTIONAL BUT RECOMMENDED]
While not necessary, I like to use the ‘Set Play Rate’ node to keep the re-center speed as a continuous speed. For example, without this, if your camera is pointed 180 degrees behind the car, the camera would quickly snap to the front, BUT if your camera was ~15 degrees to the side, it would slowly re-center. This is because no matter where your camera is, it will ALWAYS take 1 second to re-center, wether you’re looking behind you or infront of you.
Below is the code behind the ‘Camera Rot Diff’ variable - or Camera Rotation Difference.
Simply put, it finds the distance between your camera rotation, and the rotation of the vehicle. It is then normalized to be between 0 to 180 instead of -180 to 180. Directly behind the car will always be 180, and directly infront will always be 0.
We want this so we can tell our timeline how long it should take to re-center the camera, instead of just 1 second, no matter where you’re looking.
Back to the Reset Camera event, I divide the result of the Camera Rot Diff by 10 to bring the range of the float between 1 and 18. Feel free to play around and find a suitable number for you.
Finally we must divide 1 by that result. That’s why it’s important to keep the length of the Timeline to 1 second. This basically makes it so the further your camera rotation is from the vehicles rotation, the longer it takes to re-center.
I think 10 is a good divider for me, as it makes the rotation feel pretty much the same at all angles.
If you don’t understand, unfortunatley I don’t have time to make a video explaining it, but try it yourself and notice how to car smoothly re-centers at a constant rate no matter where your camera is facing.
I hope this helps future game devs!