Today i am gibing you very simple yet cool Unity 2d Camera Follow Script with smooth follow and other useful options.
Very easy to setup just add this script to your camera and then drag your character or anything you cant to follow into the script's target field, Like i added character in this field, finish :)
Script is here or get it from github.
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; | |
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 | |
target = GameObject.FindObjectWithTag("Player");
ReplyDeleteI have a weird problem, when I attach to my camera, and add to the target my player... the camera zoom to my player and don't stop.. zoom to the infinite hahahah .. any clue for what is happening?
ReplyDelete