# How can I use the velocity of a gameobject to calculate the camera position in Unity?

I am using AddForce() to make a ball move around the scene. The camera follows the ball at a fixed position. This is my camera script:

public class CameraScript : MonoBehaviour {

public GameObject target;
public float xOffset, yOffset, zOffset;

void FixedUpdate() {
Vector3 targetPosition = target.transform.position + new Vector3(xOffset, yOffset, zOffset);
transform.position = targetPosition;
transform.LookAt(target.transform.position);
}
}


This works well, except that the camera doesn’t rotate nicely around the ball. I need the camera to always be behind the player so you can clearly see what is in front of the ball (in the direction you are rolling).

Can I somehow use the velocity of the ball to get the correct camera rotation? My current ball script looks like this

public class BallController : MonoBehaviour {

private PlayerInput playerInput;
private InputAction moveAction;
private Vector3 direction;
private Rigidbody rb;
public float speed = 5f;

void Awake() {
playerInput = GetComponent<PlayerInput>();
moveAction = playerInput.actions.FindAction("Move");
rb = GetComponent<Rigidbody>();
}

void Update() {
direction = new Vector3(current.x, 0, current.y);
}

void FixedUpdate() {
}
}
$$$$


For normal characters, the camera can simply be a child of the character GameObject. For a ball that rotates via physics you can't rely on the ball rotation anymore. So it's a bit trickier:

1. You have to create an empty GameObject in your hierarchy. Then, make your camera a child of that GameObject. Reset the camera transform in Insepctor (so all values are 0). Offset the camera to a distance you need simply by changing it's position values in Inspector.

2. Next step is to rewrite your scripts to simply copy the ball position to the parent of the camera instead of the camera itself. This effectively removes complexity from the calculations, as you don't need to deal with the offset anymore.

3. Now, rotation. You definitely can use your direction value. What you are looking for is Quaternion.LookRotation. E.g.

var rotation = Quaternion.LookRotation(direction, Vector3.up);
cameraParent.transform.rotation = rotation;


LookRotation converts your direction into a quaternion, and then you set it to transform.rotation of your camera parent.

Note 1: you may want to guarantee that CameraScript always executes after your BallController`. For that, you can use Script Execution Order settings

Note 2: if you want more complex camera effects (for example, if you want acceleration and deceleration of the camera), it is probably worth looking into Cinemachine package. It includes third-person camera behaviour.

• Thanks! After some puzzling I was able to calculate the camera look direction based on the ball's velocity but that introduced new problems :) I also tried creating a separate gameobject like in your answer. In the end I settled for keeping track of the "look direction" of the ball (player) in a variable. This variable updates when the player presses left or right. And that variable is then passed to the camera to determine the camera position, so I can still use smoothing/dampening. But there are still problems, especially when falling or rolling next to a big wall. I will try Cinemachine too Commented Jul 25 at 9:24