Created
October 27, 2023 20:50
-
-
Save TheMehranKhan/5478d779db87f384184f811def7f3826 to your computer and use it in GitHub Desktop.
This code block provides a camera follow functionality in Unity. It allows the camera to smoothly follow the player object.
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
/* | |
Author: themehrankhan | |
License: MIT License | |
Description: | |
This code block provides a camera follow functionality in Unity. It allows the camera to smoothly follow the player object. | |
Usage: | |
1. Attach this script to the camera object in Unity. | |
2. Set the target object (player object) in the inspector. | |
*/ | |
using UnityEngine; | |
public class CameraFollow : MonoBehaviour | |
{ | |
public Transform target; // Target object to follow | |
public float smoothSpeed = 0.125f; // Smoothness of camera movement | |
private Vector3 offset; | |
private void Start() | |
{ | |
offset = transform.position - target.position; | |
} | |
private void LateUpdate() | |
{ | |
Vector3 desiredPosition = target.position + offset; | |
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); | |
transform.position = smoothedPosition; | |
transform.LookAt(target); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment