0
votes

I am trying to make a game where the camera follows the user, as they move around. I made the camera a child of the player and in the editor, the camera rotates around the player fine. When I play the game (in Unity) and rotate the player, the camera literally rotates instead of rotating around the player to keep the same regular following distance. I use transform.Rotate() to rotate the player by the way.

In Summary:

  1. Looking for camera that follows player in 3D game, that does not look different when the player turns.
  2. issue is that the camera appears in the editor to rotate around the player perfectly, but not when transform.Rotate() is called on it during runtime in Unity.

All help is appreciated, tyvm for help.

1

1 Answers

3
votes

I made the camera a child of the player and in the editor

Everything went down by doing this. You don't make the camera a child if you want it to follow the player.

What you do is to get the distance between the camera and the player in the Start() function. This is also called offset. In the LateUpdate() function, continuously move the camera to the Player's position, then add that offset to the camera's position. It is as simple as that.

public class CameraMover: MonoBehaviour
{
    public Transform playerTransform;
    public Transform mainCameraTransform = null;
    private Vector3 cameraOffset = Vector3.zero;

    void Start()
    {

        mainCameraTransform = Camera.main.transform;

        //Get camera-player Transform Offset that will be used to move the camera 
        cameraOffset = mainCameraTransform.position - playerTransform.position;
    }

    void LateUpdate()
    {
        //Move the camera to the position of the playerTransform with the offset that was saved in the begining
        mainCameraTransform.position = playerTransform.position + cameraOffset;
    }
}