# How to Rotate Camera Automatically Based on Pawn's Direction of Movement?

Hey all, I have a Pawn that contains a physics object. I have my camera set up on a spring arm behind my Pawn and I’m trying to get the camera to rotate in the x and y direction based on which direction my physics object is moving.

As an example: I have a marble on a racetrack. The marble has a camera placed behind it on a spring arm. As the marble moves along the racetrack, it might turn left . As it turns left I would like the camera to rotate so that we still see the racetrack ahead. Currently, when the marble turns, the camera just stays in it’s original position and all I see is the wall of the racetrack.

I believe I need to add rotation to the spring arm that is somehow “linked” to the forward vector of the marble? I just don’t know if this is the best method and how to go about doing this. Any help would be great! Thank you!

PS. “Use pawn rotation” doesn’t work because the marble is a physics object and is spinning in all directions constantly. I just want the camera to rotate based on the marbles vector movement on x and y axis.

You need to get the location vector of the prev frame and the location of the current frame, then calculate the direction vector or get velocity and normalize.

You can then turn this vector into a rotation for the camera / spring arm.

Thank you, do you have any documentation on how to obtain the vector of the previous location? I understand what needs to be done afterwards but I don’t understand how to retain the previous vector’s location for use with the “get unit direction vector” node.

You can create a variable, do the operation and then assign it the current location.

By doing the operation before assigning the value, for next frame it will do the operation with the value you assigned the previous frame, then update it for next frame, etc…

Did a quick test… it works.

You could experiment the Make Rot nodes to see if they give you same result with less nodes:

Jusr keep in mind this setup does not allow the camera to go upside down.

Thank you for the information! However I’m still stuck making the PrevLoc variable. What do I need to place as its default value?

Also, Is your arrow a child of the sphere?

You could also Get Velocity | Unreal Engine Documentation + Normalize | Unreal Engine Documentation knowing the ball will have physics enabled. This will give you a unit direction vector of the ball on every frame. No need to store value of prev frame ( but IMO it won’t hurt to understand how that logic works ).

It needs to be a child for it to move with the sphere. The alternative is to set the rotation and location on every frame. I added the arrow just to debug if the math worked. You can add how ever it works for you and delete it later.

hey there BStoneArch did you get this to work? im trying to do the same thing and having issues with not being able to get the camera to rotate.

For anyone looking into this in the future, this is my setup that I got to work based off of pezzott1’s graph but tweaked a little and with no unknown variables. The FInterps are optional but help the camera feel a bit.

2 Likes