Home > Blockchain >  Camera not following
Camera not following

Time:10-04

I just started learning game dev and now I am pretty much just doing what Brackeys is doing but I am getting the error An object reference is required for the non-static field, method or property "Transform.position" but he does not get the error. Trying to make the camera follow the player.

public class FollowPlayer : MonoBehaviour
{
      
       public static Transform player;
       

    // Update is called once per frame
    void Update()
    {
       
        Transform.position = player.position;
    }
}

CodePudding user response:

Unity is case sensitive, Transform.position = player.position; Should be transform.position = player.position;

CodePudding user response:

If you just want the player to follow the camera in call conditions then its better to position the camera at a distance from the player so that the view is good and make the camera a child of your player. This is same as what your above code does. About the error in your code. You just need to make it a small 't' in this line

transform.position = player.position;

But if the player rotates or jumps then you will not have the desired effect. If you need those to be taken care then you will need to write more codes or you can use Unity Cinemachine. This tutorial on Camera follow player covers all these topics.

  • Related