Time for actioning – attach the LookAtPlayer camera script
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:
What just happened?
Let us analyze the code that we just saw:
Line 6: private Transform playerPosition;
- The variable
playerPosition
will store theTransform
information of Player - This means that every frame, the
Transform
x, y, z position of Player is updated and stored in the variableplayerPosition
Line 10: playerPosition = GameObject.Find("Player").transform;
- In order to store the
transform
position of Player, the script first needs a reference of the Player GameObject by usingGameObject.Find("Player")
- Then the
transform
position of Player is retrieved and stored in the variableplayerPosition
Line 13: void LateUpdate( )
Lookup LateUpdate in Scripting Reference, here's a...