Main Camera follows player?

I have a script attached to my main player, and I want to set the Main Camera to follow behind my player in the same script. Is there some way to do this by using the players position minus 1.5 for the cameras position?

I think I need to use a target variable correct? How would I do that?

All you need to do, is make the Main Camera a child of the Main Character object (drag and drop in the hierarchy menu.) Then, set the camera’s transform to (0, 0, -1.5).

As a child, the camera will move when the player moves, and it’s position will use the player’s position, modified by the -1.5.

Meaning, if your player is on X: 0, Y: 0, Z: 0, the camera will be X:0, Y:0, Z: -1.5.
If the player moves to 100, 100, 100, the camera will be 100, 100, 103.5

No need for any scripting to do this.

Take a look at the SmoothFollow in the Standard Assets scripts, which is included with the free version of Unity. That allows you to set a follow distance and height, and will smoothly follow your character around.

You apply this script to the camera, and use the inspector to set the follow distance, height, and target (the player game object in your case).

Note: if you don’t install the standard assets with Unity you can download them from the Asset Store.

@aidenkael
You don’t have to do this by scripting because if you make your camera a child of the player it will automatically follow the player. However if you want to make it by scripting you can use the below C# code:

void Update () {
    // Temporary vector
    Vector3 temp = player.transform.position;
    temp.x = temp.x - x;
    temp.y = y;
    temp.z = temp.z - z;

    // Assign value to Camera position
    transform.position = temp;
}

where x, y, z are the variables which define the difference of the camera position to that of the player.

hey im new to unity, exactly which XYZ things do i hav to change for the camera, i just see three options @Aspirer

someone who is finding to camera follow the player, Here you can find the smooth camera follow:- https://gamedevsolutions.com/smooth-camera-follow-in-unity3d-c/