This simple script makes the camera look at Player. The camera does not move. It simply rotates so it's always looking at Player no matter how close or far away it is.
Attach this script to LookAt Camera in the Hierarchy panel:
Let us analyze the code that we just saw:
Line 6: private Transform playerPosition;
Line 10: playerPosition = GameObject.Find("Player").transform;
transform
position of Player, the script first needs a reference of the Player GameObject by using GameObject.Find("Player")
transform
position of Player is retrieved and stored in the variable playerPosition
Line 13: void LateUpdate( )
Lookup LateUpdate in Scripting Reference, here's a quote:
...a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
Line 15: transform.LookAt(playerPosition);
18.191.176.99