Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
10
Question by Oninji · Oct 05, 2010 at 05:52 PM · camera2dside-scrolling

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

9 Replies

· Add your reply
  • Sort: 
avatar image
43

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);
         }
     
     }
 }
Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image frankmail007 · Dec 16, 2013 at 05:14 AM 0
Share

Great! Solve my issue. I don't need to use FirstPerson Controller anymore to use this script.

avatar image Alienguard · Jan 31, 2014 at 02:38 AM 0
Share

Would you $$anonymous$$d if I use it for my game? I tested and it works like a charm.

avatar image vbs · Sep 21, 2014 at 10:20 PM 0
Share

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.

avatar image jonrod29 · Jan 08, 2015 at 10:32 PM 0
Share

Hey thanks a bunch for this script ! Nice and smooth, just what I needed ;D

avatar image Rodiaz89 · Feb 05, 2015 at 08:39 PM 2
Share

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 :)

Show more comments
avatar image
12

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JGriffith · May 01, 2013 at 10:41 AM 0
Share
  • to this, thanks so much. I tried so many things to figure out why this was happening. No amount of smoothing was helping.

avatar image ChaosD · Nov 17, 2013 at 07:39 PM 0
Share

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?

avatar image vaticanwilliam · Jan 26, 2014 at 02:50 AM 0
Share

Thank's very much for this. Solved my problem

avatar image huja · Jan 10, 2015 at 09:20 AM 0
Share

Thank you! Solved my problem as well. Was searching for hours today.

avatar image ccx217 · Dec 13, 2015 at 06:24 AM 1
Share

or you could just change it to FixedUpdate ins$$anonymous$$d of Update :P

Show more comments
avatar image
11

Answer by skovacs1 · Oct 05, 2010 at 07:54 PM

You can use whatever damping function you feel appropriate, but generally, you would repeat:

  1. figure out which way to move
  2. figure out and damp how much to move to keep the same screen position
  3. 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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rovalin · May 20, 2012 at 10:36 PM 0
Share

Thanks this was just what I was looking for!

-Rov

avatar image Seyyed · Jan 10, 2013 at 08:30 AM 0
Share

works perfect! thanks

avatar image AustinK · Apr 10, 2014 at 05:10 AM 0
Share

@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.

avatar image
7

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);
  
         }
     }
 }


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MoliCat · Feb 21, 2015 at 10:38 PM 1
Share

Hey Jennifer you are awesome you gave me this cool script :) thanks

avatar image Nananaaa · Jul 28, 2016 at 09:23 AM 0
Share

Nice script. Thank you!

avatar image
4

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);
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Determine if GameObject with no renderer is within Orthographic camera bounds 1 Answer

Why is my 2D Sprite squashed during Play? 0 Answers

Isometric - Camera follow an object when at the edge of the screen 1 Answer

Move an object in world space on the viewport 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges