Camera follow player script, determining the offset

I remember the very first tutorial I did here to learn C#, Roll a Ball. It had a camera following the ball and the code used looked roughly like that:

using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    [SerializeField] private GameObject plane;

    private Vector3 offset;

    // Start is called before the first frame update
    void Start()
    {
        // The offset is determined by the start position of the camera
        // minus the player transform position
        offset = transform.position - plane.transform.position;
    }

    void LateUpdate()
    {
        transform.position = plane.transform.position + offset;
    }
}

Remaking the Create with Code course in order to practice the new input system, I noticed that the instructors no longer use

        offset = transform.position - plane.transform.position;

they use a Vector3() value instead (they don’t explain how they get the value).

Is there a reason for that? Something performance-wise?

Thanks!

That’s just a constant offset now. Previously it was the distance between two objects, which is variable depending on how the scene is set up. Using the constant ensures the code runs exactly the same for every user.

Looks like they just hardcoded it instead of computing it from given Transforms.

Speculating, it’s likely because people struggled to get the ball and the camera offset correct in the first place but that’s just my guess. I’m not a mind-reader.

While you’re on the subject of Camera controllers…

Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: Unity Engine - Unity Discussions

If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.

For big projects, yes I would but not for such a simple thing as a tutorial. :slight_smile:

Thanks for the script, I’ll look at it. For now, I use:

    private void Start()
    {
        transform.SetPositionAndRotation(new Vector3(3.5f, 5f, -47.5f), Quaternion.Euler(0f, 0f, 0f));

        offset = transform.position - vehicle.transform.position;
    }

Simple enough and it does the job. :wink:

Interesting approach.

I prefer to learn about complex things in simpler contexts when I can control the rate of adoption.

This is especially true if it will save me from my boss coming in and saying “How come you haven’t figured out how to use Cinemachine yet dood?!? We have to ship this game by EOD and you’re holding us up!”

YMMV. :slight_smile:

Well, being independent, nobody will boss me around and I’ll learn Cinemachine when I feel like it. :wink:
It’s not completely unknown to me though, I still would be able to create a follow camera, set up boundaries, the minimum. :slight_smile:

That said, it’s probably more practical if we place the camera behind the player manually. It makes for a more simpler script.

using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    [SerializeField] private GameObject vehicle;

    private Vector3 offset;


    // Start is called before the first frame update
    private void Start()
    {
        offset = transform.position - vehicle.transform.position;
    }

    // Update is called once per frame
    private void LateUpdate()
    {
        transform.position = vehicle.transform.position + offset;
    }
}