The easiest way to get one GameObject to follow another is to make one of them a child of the other. However, this approach means that any kind of movement or rotation that happens to the player capsule also affects the camera, which is something we don't necessarily want. Luckily, we can easily set the position and rotation of the camera relative to the capsule with methods from the Transform class.
Time for action – Scripting camera behavior
Since we want the camera behavior to be entirely separate from how the player moves, we'll be controlling where the camera is positioned relative to a target we can set from the Inspector:
- Create a new C# script in the Scripts folder, name it CameraBehavior, and drag it onto the Main Camera.
- Add the following code and save it:
public class CameraBehavior : MonoBehaviour
{
// 1
public Vector3 camOffset = new Vector3(0, 1.2,...