Spoiler - The easy way to make camera look at player

I did it the hard way…

  void Start() {
        player = GameObject.FindGameObjectWithTag("Player");
        camera = Camera.main;
    }

    void LateUpdate() {
        Vector3 direction = player.transform.position - camera.transform.position;
        camera.transform.rotation = Quaternion.FromToRotation(Vector3.forward, direction);
        float angle = -camera.transform.rotation.eulerAngles.z;
        camera.transform.Rotate(Vector3.forward, angle);
    }

It worked, but it took several iterations to get it right. That is one advantage of google when you’re programming: you could find the easy solution instead of building it yourself like I did. But if you’re using something other than Unity, now you have an outline of how to write a “look at” function.

The easy way, of course, is to use transform.LookAt (as seen in the video after you unpause from the challenge), which has this reinvented wheel already packaged up.

Privacy & Terms