Skip to content

Instantly share code, notes, and snippets.

@TheMehranKhan
Created October 27, 2023 20:50
Show Gist options
  • Save TheMehranKhan/5478d779db87f384184f811def7f3826 to your computer and use it in GitHub Desktop.
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.
/*
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