Menu
CoddyTech

Move Where You Look

Part of the Building Real Games section of Coddy's Unity journey. Lesson 41 of 56.

When the camera looks at the player from an angle, the arrows should move the player as the screen shows it: up walks away from the camera, right walks to the camera's right. The world's axes only do that while the camera looks along +z. So build the move from the camera's own directions, cam.forward and cam.right.

They need cleaning first. A camera that looks down at the player has a forward that points into the ground, so set its y to 0 to keep only the part along the ground. What is left is shorter than 1, about 0.87 for a camera tilted down 30 degrees, and the player would walk slower, so normalize it. This walker follows the main camera, which looks down at a corner of the level:

Transform cam = Camera.main.transform;

Vector3 forward = cam.forward;
forward.y = 0f;
forward.Normalize();
Vector3 right = cam.right;
right.y = 0f;
right.Normalize();

Vector3 move = forward * Input.GetAxisRaw("Vertical") + right * Input.GetAxisRaw("Horizontal");
if (move.magnitude > 1f) move.Normalize();   // diagonals no faster
controller.Move(move * speed * Time.deltaTime);

Camera.main is the camera tagged MainCamera. The two directions are read every frame, so the controls stay right when the camera turns. Up and right together add two unit vectors at right angles, which is about 1.41 long, so the move is normalized when it is longer than 1.

challenge icon

Challenge

Medium

The camera slowly circles the player (CameraRig.cs, ready), so the world's axes keep turning on screen. Make the arrows walk the player relative to the camera in Mover.cs, where gravity is already written. The Inspector sets speed (4) and cam, the camera's Transform.

  • Up walks away from the camera along the ground, down toward it, right and left to the camera's right and left: speed units a second in every direction, diagonals included, though the camera looks down at the player.
  • While it walks, the player turns to face the way it walks. When it stops, it keeps facing that way.

Press Play, walk around while the camera turns, then Check.

Try it yourself

using UnityEngine;

public class Mover : MonoBehaviour
{
    public float speed = 4f;
    public float gravity = -15f;
    public Transform cam;

    CharacterController controller;
    float verticalSpeed;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        if (controller.isGrounded && verticalSpeed < 0f)
        {
            verticalSpeed = -2f;
        }
        verticalSpeed += gravity * Time.deltaTime;

        // Walks along the world's axes: make it walk relative to cam, and face the way it walks
        Vector3 move = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")) * speed;
        move.y = verticalSpeed;
        controller.Move(move * Time.deltaTime);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Building Real Games