[SOLVED] matching camera forward and player forward while rotating

Hi, here is my problem.

When i rotate the camera and try to go forward, the character movement don’t care about the camera position and his forward is not the same as the camera’s forward.

for example if I move forward, then turn 90° around the character with the camera and try to move forward again, the player goes to the left not forward according to the camera’s position.

the camera rotation script

public class CameroFollow : MonoBehaviour
{
    public Transform target;
    public Vector3 offset;
    public float rotateSpeed;

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

    // Update is called once per frame
    void Update()
    {
        // if there is no target
        if (target == null) return;

        offset = Quaternion.AngleAxis(Input.GetAxis("Fire1") * rotateSpeed, Vector3.up) * offset;
        transform.position = target.position + offset;
        transform.LookAt(target.position);

    }
}

the player movement script

public class PlayerController : MonoBehaviour {
    public float moveSpeed;
    private Rigidbody rig;

    // Awake is called before game starts
    void Awake() {
        //get the Rigidbody component
        rig = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update() {
        Move();
    }

    void Move() {
        float xInput = Input.GetAxis("Horizontal");
        float zInput = Input.GetAxis("Vertical");

        Vector3 dir = new Vector3(xInput, 0, zInput) * moveSpeed;
        dir.y = rig.velocity.y;

        rig.velocity = dir;

        //rotating character
        Vector3 facingDir = new Vector3(xInput, 0, zInput);

        if (facingDir.magnitude > 0) { transform.forward = facingDir; }
    }
}

I hope i explain my issue well, english is not my native language.
Thanks for your help.

Make your camera follow script change the Y rotation of your player’s transform, and change the camera’s X rotation. It might not be a permanent fix but it works for most people.

Once again i can recommend the character / camera series by Sebastian Lague for these kinds of problems:

I believe this is the behavior you are looking for?

2 Likes

Yes that what’s i’m looking for, thanks.

Edit: I’ve just tried and it works very well !