# 2D Camera "Smooth Follow"

 0 Hellow, I'm trying to make a Camera in Orhtographic and make its focus on y and x plane. But I want to delay the focus as to create a damping motion in the camera movement. How would I achieve that? more ▼ asked Oct 05 '10 at 05:52 PM Oninji 332 ● 43 ● 47 ● 50 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 You can use whatever damping function you feel appropriate, but generally, you would repeat: figure out which way to move figure out and damp how much to move to keep the same screen position move If you threw it into a coroutine with a wait at the start, you could add inital delay and other things, but generally that's it. Here is a simple damp function that will do generally what you need using Vector3.SmoothDamp: ``````var dampTime : float = 0.3; //offset from the viewport center to fix damping private var velocity = Vector3.zero; var target : Transform; function Update() { if(target) { var point : Vector3 = camera.WorldToViewportPoint(target.position); var delta : Vector3 = target.position - camera.ViewportToWorldPoint(Vector3(0.5, 0.5, point.z)); var destination : Vector3 = transform.position + delta; transform.position = Vector3.SmoothDamp(transform.position, destination, velocity, dampTime); } } `````` It's been written to work for both orthogonal and perspective cameras. If you want, you can apply some optimization specifically for orthogonal because you wouldn't need to calculate viewport points for an orthogonal camera. more ▼ answered Oct 05 '10 at 07:54 PM skovacs1 10k ● 11 ● 25 ● 91 Thanks this was just what I was looking for!-Rov May 20 '12 at 10:36 PM Rovalin works perfect! thanks Jan 10 at 08:30 AM Seyyed add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 Here is Scott's answer in C# ``````using UnityEngine; using System.Collections; public class SmoothCamera2D : MonoBehaviour { public float dampTime = 0.15f; private Vector3 velocity = Vector3.zero; public Transform target; // Update is called once per frame void Update () { if (target) { Vector3 point = camera.WorldToViewportPoint(target.position); Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z)); //(new Vector3(0.5, 0.5, point.z)); Vector3 destination = transform.position + delta; transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime); } } } `````` more ▼ answered Oct 11 '12 at 06:48 PM amaranth 17 ● 4 ● 4 ● 6 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 This is probably not needed at this point but just in case someone stumbles upon this answer, this is the modified code needed to lock the Y-Axis of the camera, just like an old school platformer. All original code by Scott Kovacs. ``````//Written by Scott Kovacs via UnityAnswers.com; Oct 5th 2010 //2DCameraFollow - Platformer Script var dampTime : float = 0.3; //offset from the viewport center to fix damping private var velocity = Vector3.zero; var target : Transform; function Update() { if(target) { var point : Vector3 = camera.WorldToViewportPoint(target.position); var delta : Vector3 = target.position - camera.ViewportToWorldPoint(Vector3(0.5, 0.5, point.z)); var destination : Vector3 = transform.position + delta; // Set this to the Y position you want the camera locked to destination.y = 0; transform.position = Vector3.SmoothDamp(transform.position, destination, velocity, dampTime); } } `````` more ▼ answered Apr 08 '12 at 02:50 AM Sitrane 16 ● 8 ● 10 ● 12 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 I would like to add, if rigidbody object you folow moves not smoothly (jittery), even if camera moves smooth. Turn on Interpolation in properties of rigid body. I couldn't figure out this for months (You can read more about this here: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-interpolation.html more ▼ answered Dec 12 '12 at 03:14 AM +1 to this, thanks so much. I tried so many things to figure out why this was happening. No amount of smoothing was helping. May 01 at 10:41 AM JGriffith add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x2994
x1034
x53

asked: Oct 05 '10 at 05:52 PM

Seen: 9326 times

Last Updated: May 01 at 10:41 AM