Camera following player

Hello, i’m a total begineer with programation and i want to make that the camera is following player so i use a script that i found in a tutorial, this one. " Movement Basics - Unity Learn " The problem is that when i rotate the camera is not rotating. I added this line transform.rotation = player.transform.rotation; but the move are realy weird.


I tried transform.rotation = player.transform.rotation + offset; but its saying “cannot apply operator + to operands of type quaternion and vector3”, someone can help me please? sorry if my english is bad im french and here is my script

using UnityEngine;
using System.Collections;

public class CompleteCameraController : MonoBehaviour
{

    public GameObject player;       //Public variable to store a reference to the player game object


    private Vector3 offset;         //Private variable to store the offset distance between the player and camera

    // Use this for initialization
    void Start()
    {
        //Calculate and store the offset value by getting the distance between the player's position and camera's position.
        offset = transform.position - player.transform.position;
    }

    // LateUpdate is called after Update each frame
    void LateUpdate()
    {
        // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
        transform.position = player.transform.position + offset;
        transform.rotation = player.transform.rotation;
    }
}

If you just want the camera to stay a fixed distance from the player and always point in a direction where the player would be in view, you can just make the camera a child of the player.

As for your script instead, if you want it to be a 3rd person camera behind your player, you’re probably going to want to use the player’s local position rather than global, then convert the player’s local position + offset to world space position, so you can keep the camera positioned behind the player as the player turns. Your rotation code looks fine to me if the player stays on a flat plane, but I’m a fan of just placing an object above the player and having the camera look at that object.

The rotation is probably choppy? Using Vector3.SmoothDamp() for example would solve this, but I explain a much much easier way at the end.

As it’s said, transform.rotation has the type Quaternion and offset the type Vector3. What you want to use instead is transform.eulerAngles, which is the X, Y, Z representation of the rotation like in the editor.
transform.eulerAngles = player.transform.eulerAngles + offset;

But as the guy who was a little bit faster than me ( :hushed: ) said, the easiest way is simply making the camera a child of the player and then adjusting the offset in the editor by changing the camera’s position (which is then the camera’s local position relative to the player).

Ok thank you guys its working :slight_smile:

Hey so me and my group are trying to code our camera so it can follow one player then when their turn is done follows the second player then go back to the first player and so on and so forth. We need help with how we need to code this.

Use Cinemachine. There are a tons of tutorials about it on YouTube.

1 Like

Don’t necro/hijack threads when you can simply create your own.

Thanks.