Time for action – attaching the FollowingPlayer camera script
This camera script has three added lines of code to make the camera move as Player moves.
Attach this script to Following Camera in the Hierarchy panel:
What just happened?
An analysis of the code shown in the preceding screenshot is as follows:
Line 6: public float cameraHeight = 17.0f;
- The variable
cameraHeight
stores the distance the camera will be above Player
Line 7: public float cameraDistance = 17.0f;
- The variable
cameraDistance
stores the distance the camera will be away from Player on the x and z axes
Line 18: transform.position = playerPosition.position +
new Vector3(cameraDistance, cameraHeight, -cameraDistance);
- The variable
playerPosition
stores thetransform
data of Player, but all we want to know is theposition
data from all thattransform
data - Therefore,
playerPosition.position
stores the constantly changing x, y, z position of Player - The height and distance data stored in
cameraHeight
andcameraDistance...