Created
February 19, 2024 15:30
-
-
Save leventeren/d268e3ce9a2e607988685dcc32ed552c to your computer and use it in GitHub Desktop.
Camera Follow Character with forward offset
This file contains 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; | |
public float smoothSpeed = 0.125f; | |
public Vector3 offset; | |
public float forwardOffset = 2f; | |
private Vector3 desiredPosition; | |
private Quaternion desiredRotation; | |
void LateUpdate() | |
{ | |
if (target == null) | |
return; | |
desiredPosition = target.position + offset; | |
transform.position = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed); | |
Vector3 lookAtPosition = target.position + target.forward * forwardOffset; | |
desiredRotation = Quaternion.LookRotation(lookAtPosition - transform.position); | |
transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotation, smoothSpeed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment