Skip to content

Instantly share code, notes, and snippets.

@unity3diy
Last active January 15, 2024 18:05
Show Gist options
  • Save unity3diy/5aa0b098cb06b3ccbe47 to your computer and use it in GitHub Desktop.
Save unity3diy/5aa0b098cb06b3ccbe47 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
@NickDeGrood
Copy link

Is there a way to increase the follow speed? The object I'm tracking is following too fast, and the camera can't keep up with it.

@Theo-Farnole
Copy link

NickDeGrood try to replace "FixedUpdate" by "Update", answer if it worked ;)

Copy link

ghost commented Jun 28, 2018

Lortedo29 is incorrect for the camera movement, but correct in the respect that if you are not applying physics then you should be using Update instead of FixedUpdate.

@NickDeGrood
create a public float speed = 15f; at the top
then replace
interpVelocity = targetDirection.magnitude * 5f;
with
interpVelocity = targetDirection.magnitude * speed;

EDIT:
Here is another gist of this... cleaned up, added speed, changed to update, fixed offset

@etzl
Copy link

etzl commented Nov 30, 2018

This have a bug or didn't work for me and i think bug is where it says: targetPos + offset and i have a solution:


targetPos = target.TransformPoint(offset);

this can replace with all lines above Lerp function.

@nicmar
Copy link

nicmar commented Jan 17, 2019

Nice, but minDistance isn't used? :) I'm trying to make it predict where the player is going.. but not sure if minDistance is for that :)

Copy link

ghost commented Aug 13, 2021

thanks, this works great!

@Aquatiks
Copy link

TYSMMM ive been on this for like 10 hours

@fatmaAbdelaal
Copy link

that saved me a LOT
thanks man, awesome work

@omaelcalan
Copy link

My opinion is that you do not have to pull the player to the camera because the movement is affected by the camera follow so do not drag the character to the camera

@willilamotte98
Copy link

Thank you so much for this!

@eri6081
Copy link

eri6081 commented Jun 14, 2023

What do you have to change in the settings like Offset etc.?

@fionawantsboobs
Copy link

fionawantsboobs commented Jun 24, 2023

thank you random stranger i am too shit at coding you really saved me <3<3<3
lots of love from Fiona

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment