Skip to content

Instantly share code, notes, and snippets.

@leventeren
Created February 19, 2024 15:30
Show Gist options
  • Save leventeren/d268e3ce9a2e607988685dcc32ed552c to your computer and use it in GitHub Desktop.
Save leventeren/d268e3ce9a2e607988685dcc32ed552c to your computer and use it in GitHub Desktop.
Camera Follow Character with forward offset
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