Created
December 19, 2022 07:38
-
-
Save AmmarTee/b3a84b863e59de91fc01ee4657625d40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class CameraFollow : MonoBehaviour | |
{ | |
public Transform target,MainCamera; // The object that the camera should follow | |
public float smoothSpeed = 0.125f; // The speed at which the camera should follow the target | |
public Vector3 offset; // The offset from the target's position that the camera should follow | |
// Start is called on the frame when a script is enabled just before any of the Update methods is called the first time. | |
protected void Start() | |
{ | |
MainCamera = GetComponentInChildren<Camera>().transform; | |
} | |
void LateUpdate() | |
{ | |
// Calculate the desired position for the camera | |
Vector3 desiredPosition = target.position + offset; | |
// Smoothly move the camera towards the desired position | |
MainCamera.position = Vector3.Lerp(MainCamera.position, desiredPosition, smoothSpeed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment