- Home /
2D Camera "Smooth Follow"
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?
Answer by AntFitch · Oct 11, 2012 at 06:48 PM
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);
}
}
}
Great! Solve my issue. I don't need to use FirstPerson Controller anymore to use this script.
Would you $$anonymous$$d if I use it for my game? I tested and it works like a charm.
Would I attach this script to the camera? Currently my camera is under the player object hierarchy, so it follow exactly where the player goes because of that, but if I wanted to use this script would I have to take the camera out of the hierarchy? (EDIT) I figured it out, just for posterity, the Camera should not be under the character hierarchy, and the Target parameter should be set to the "thing" you want the camera to follow.
Hey thanks a bunch for this script ! Nice and smooth, just what I needed ;D
I would change the Update to FixedUpdate, it made it so it didn't jump around if the target was at a great velocity. Awesome script either way :)
Answer by toxicdan · Dec 12, 2012 at 03:14 AM
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
to this, thanks so much. I tried so many things to figure out why this was happening. No amount of smoothing was helping.
I have the same problem, i have a player moving around by using AddForce(). Without interpolation, the camera is smooth but the player lags. If i enable it, the player just gets slower and lags even more. Extrapolate makes my player move very fast in a jumping motion. Any ideas what else i could try?
Thank you! Solved my problem as well. Was searching for hours today.
or you could just change it to FixedUpdate ins$$anonymous$$d of Update :P
Answer by skovacs1 · Oct 05, 2010 at 07:54 PM
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.
@skovacs1 - I know this is an old post, but I'll ask and just hope for an answer. What are these optimizations that can be made for orthogonal only? I've been trying to understand why viewport calculations wouldn't be necessary, but it's not clicking.
Thanks.
Answer by Jennifer1 · Feb 21, 2015 at 10:22 PM
Here is a simple and effective camera smooth follow script with few more options you should see..source github
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);
}
}
}
Hey Jennifer you are awesome you gave me this cool script :) thanks
Answer by Sitrane · Apr 08, 2012 at 02:50 AM
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);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613074817im_/https://answers.unity.com/themes/thub/images/avi.jpg)