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, ...

Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.