Search code examples
c#unity-game-engineunityscript

What is the best way follow to player with camera?


I started to learn Unity3d two days ago, so I am beginner in Unity. I try to follow to player with camera, it works but player is shaking when moving. Here is my Camera and Player move scripts.

public class CameraFollow : MonoBehavior 
{
     public Transform player;

     void update()
     {
          transform.position = new Vector3(player.position.x, player.position.y + 15, player.position.z - 3);
     }
}

public class PlayerMove : MonoBehavior 
{
     public float speed = 5f;

     void update()
     {
          transform.Translate(0f, 0f, Time.deltaTime * speed);
     }
}

Solution

  • Instead of having two movement scripts, delete the cameraFollow script, then put the Camera inside the Player's object. When the player moves/rotates, the camera will move and rotate accordingly, but will stay at the exact same location relative to the player, thus the player doesn't move in the camera.

    So, place the camera inside the player's object, then change it's coordinates so that the camera is at the correct location.