Skip to content

Instantly share code, notes, and snippets.

@adoankim
Forked from unity3diy/unity 2d camera follow
Created November 14, 2015 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adoankim/720452a714a4b542bc3c to your computer and use it in GitHub Desktop.
Save adoankim/720452a714a4b542bc3c to your computer and use it in GitHub Desktop.
unity 2d camera follow script, add this script to camera and drag character or your object into it. simple!
using UnityEngine;
using System.Collections;
public class FollowCamera : MonoBehaviour {
public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start () {
targetPos = transform.position;
}
// Update is called once per frame
void FixedUpdate () {
if (target)
{
Vector3 posNoZ = transform.position;
posNoZ.z = target.transform.position.z;
Vector3 targetDirection = (target.transform.position - posNoZ);
interpVelocity = targetDirection.magnitude * 5f;
targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime);
transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);
}
}
}
// Original post with image here > http://unity3diy.blogspot.com/2015/02/unity-2d-camera-follow-script.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment