Advertisement
johnnygoodguy2000

CameraFollow.cs

Dec 25th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | Gaming | 0 0
  1. using UnityEngine;
  2.  
  3. public class CameraFollow : MonoBehaviour
  4. {
  5.     public Transform target;  // Reference to the player character
  6.     public Vector3 offset = new Vector3(0, 0, -10);  // Offset to maintain distance
  7.     public float smoothSpeed = 0.125f;  // Smoothing factor for the follow movement
  8.  
  9.     private void LateUpdate()
  10.     {
  11.         if (target != null)
  12.         {
  13.             // Calculate the desired position with offset
  14.             Vector3 desiredPosition = target.position + offset;
  15.  
  16.             // Smoothly interpolate the camera’s position to the target position
  17.             Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
  18.  
  19.             // Set the camera’s position
  20.             transform.position = smoothedPosition;
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement