Skip to content

Instantly share code, notes, and snippets.

@ASPePeX
Created March 3, 2017 11:22
Show Gist options
  • Save ASPePeX/189068d9aa02e454a58f7c6d6e860fb3 to your computer and use it in GitHub Desktop.
Save ASPePeX/189068d9aa02e454a58f7c6d6e860fb3 to your computer and use it in GitHub Desktop.
An easy and short script for a smooth 2D follow camera in Unity.
using UnityEngine;
public class SmoothCameraFollow2D : MonoBehaviour
{
public GameObject Target;
public float LerpValue = 0.05f;
private float _t;
private Vector3 _pos;
void Start()
{
_pos = this.transform.position;
}
void FixedUpdate()
{
if (Target != null)
{
_t = LerpValue;
_pos.y = Mathf.Lerp(this.transform.position.y, Target.transform.position.y, _t);
_pos.x = Mathf.Lerp(this.transform.position.x, Target.transform.position.x, _t);
this.transform.position = _pos;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment