Let the camera follow the character's movement in Game is quite often to see in games, such as Super Mario. Luckily Unity make it quite easy to implement by making the camera's transform follow the position and rotation of the target game object.
One example of how:
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target; // The target to follow
public float smoothSpeed = 0.125f; // The speed at which the camera should follow the target
public Vector3 offset; // The offset from the target position
void FixedUpdate()
{
// Get the desired position of the camera based on the target's position and the offset
Vector3 desiredPosition = target.position + offset;
// Smoothly move the camera towards the desired position
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
}
}
You can attach this script to the camera object in your scene and set the target in the Inspector. The offset
can be used to adjust the position of the camera relative to the target. The smoothSpeed
determines how fast the camera should follow the target.
In this example, the camera follows the target using the FixedUpdate
method. You can also use the Update
method, but it's recommended to use FixedUpdate
when dealing with physics-based movement.