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 tab:
- Create a new C# script in the Scripts folder, name it CameraBehavior, and drag it into the Main Camera.
- Add the following code and save it:
public class CameraBehavior : MonoBehaviour
{
// 1
public Vector3 camOffset = new Vector3(0f, 1.2f, -2.6f);
// 2
private Transform target;
void Start()
{
// 3
target = GameObject.Find("Player").transform;
}
// 4
void LateUpdate()
{
// 5
this.transform.position = target.TransformPoint(camOffset);
// 6
this.transform.LookAt(target);
}
}
Here's a breakdown of the preceding code:
- Declares a Vector3 variable to store the distance we want between the Main Camera and the Player capsule...