Camera Relative Movement in Unity

How to create Camera Relative movement in Unity

In Input, Movement by John FrenchPublished Leave a Comment

It can be quite straightforward to move an object in world space meaning that its forward direction, for example, is the same as forward in the world.

Likewise, it’s also fairly simple to move an object relative to itself, where forward means whichever way the object happens to be facing.

However, how can you create movement that is relative to a different object?

Such as the camera, for example.

Camera relative movement means that an upwards input might mean moving away from the camera’s view, while a downwards input might mean moving towards the screen.

In Unity, it’s possible to create camera relative movement by using the camera’s forward vector in place of the object’s.

Like this:

Transform cam;
public float speed = 2;

private void Start()
{
    cam = Camera.main.transform;
}

void Update()
{
    Vector3 forwardMovement = cam.forward * Input.GetAxis("Vertical");
    Vector3 horizontalMovement = cam.right * Input.GetAxis("Horizontal");

    Vector3 movement = Vector3.ClampMagnitude(forwardMovement + horizontalMovement, 1);
    transform.Translate(movement * speed * Time.deltaTime, Space.World);
}

Because the movement is relative to the camera’s position, the Relative To parameter in the Translate function needs to be set to World Space for this to work.

This is because Transform Translate operates in local space by default.

While this method will work, there’s a problem…

The forward vector of a camera that’s pointing at the player may, in some cases, be facing downwards at an angle.

This means that any movement that’s applied will also push the object downwards, into the floor, instead of along a flat plane, as you might expect it to.

This means that moving an object in relation to the camera’s exact rotation won’t always work.

Instead, you’ll need to manually calculate the direction between the camera and the object, while leaving out any rotation that you want to ignore. 

To calculate a direction, all you need to do is subtract the position of the origin from the position of the target and then normalise the result.

Like this:

Vector3 direction = (transform.position - Camera.main.transform.position).normalized;

However, right now, this doesn’t really change anything since, chances are, the camera is higher than the player and is looking down towards it. 

Meaning that, in order to create camera relative movement that works in the way you’d expect it to, you’ll need to offset the difference in height when calculating in the direction.

To ignore the difference in height between the camera and the object it’s looking at, simply create a new Vector 3 that replaces the camera’s height with the object’s instead.

Like this:

Transform camTransform = Camera.main.transform;

Vector3 camPosition = new Vector3(
    camTransform.position.x, 
    transform.position.y, 
    camTransform.position.z);
Vector3 direction = (transform.position - camPosition).normalized;

This will return a direction that is looking towards the object, but isn’t looking up or down.

You can then use the corrected direction that’s created to calculate movement that’s relative to the camera on a flat plane.

Like this:

Transform cam;
public float speed = 2;

private void Start()
{
    cam = Camera.main.transform;
}

void Update()
{
    Vector3 camPosition = new Vector3(cam.position.x, transform.position.y, cam.position.z);
    Vector3 direction = (transform.position - camPosition).normalized;

    Vector3 forwardMovement = direction * Input.GetAxis("Vertical");
    Vector3 horizontalMovement = cam.right * Input.GetAxis("Horizontal");

    Vector3 movement = Vector3.ClampMagnitude(forwardMovement + horizontalMovement, 1);

    transform.Translate(movement * speed * Time.deltaTime, Space.World);
}
John French profile picture

by John French

Game audio professional and a keen amateur developer.

Get Game Development Tips, Straight to Your inbox

Get helpful tips & tricks and master game development basics the easy way, with deep-dive tutorials and guides.

How to CODE in Unity. Available to Pre Order Now. Game Dev Beginner.

Leave a Comment