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:

Time for action – attaching the FollowingPlayer camera script

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 the transform data of Player, but all we want to know is the position data from all that transform data
  • Therefore, playerPosition.position stores the constantly changing x, y, z position of Player
  • The height and distance data stored in cameraHeight and cameraDistance can be added to the data of Player to specify where the camera should be located
  • The variables are plugged into the Vector3 x, y, z positions and added to the position of Player
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.50.222