• 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
0

Answer by Lazdude17 · Jun 04, 2014 at 09:47 PM

I'd like to add that this still made my camera follow all jittery. It was the camera and not the player, but placing it in FixedUpdate() fixed the jitteriness. I'm assuming it's because FixedUpdate runs on the same steps as Time.deltaTime.

 //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 FixedUpdate() {
     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 · Show 1 · 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 hayer · Jun 17, 2014 at 09:20 AM 0
Share

Time.deltaTime will in Update() be set to the correct delta time for update. When used in FixedUpdate() Unity will, in the background, set it to the correct delta time for fixed update.

Therefor; always use Time.deltaTime - since it will make your ti$$anonymous$$g math both correct in Update() and FixedUpdate().

http://docs.unity3d.com/ScriptReference/Time-deltaTime.html 4th paragraph.

avatar image
0

Answer by Ofx360 · Jun 15, 2014 at 06:31 PM

None of these were working for me. All combinations of the solutions posted here still resulted in jerky camera movement. So I attempted my own and came up with something a lot more simple and it works silky smooth:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCamera2D : MonoBehaviour {
     
     public float dampTime = 0.15f;
     public Transform target;
 
     void Update () 
     {
         if (target)
         {
             Vector3 from = transform.position;
             Vector3 to = target.position;
             to.z = transform.position.z;
 
             transform.position -= (from-to)*dampTime*Time.deltaTime;
         }
     }
 }

I don't have interpolate turned on my player, but it still works like butter for me. Though, i guess turning it on wouldn't hurt.

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 TheFrozenFires · Jan 02, 2015 at 01:42 PM 0
Share

Thanks! works great for unity 2d mode

avatar image firstlast122 · Oct 01, 2016 at 07:35 AM 0
Share

Thanks! Out of all solutions in this thread, this is the only one that worked for me (2D game).

avatar image
0

Answer by Tezza48 · May 17, 2015 at 07:56 PM

I use a much smaller "function" that uses Vector2s public var player : Transform; private var lerpTime : float = 5;

 function Start () {
     player = GameObject.Find("Player").GetComponent(Transform);
 }
 
 function Update () {
     transform.position = Vector2.Lerp(transform.position, player.position, lerpTime * Time.deltaTime);
 }

Works great but it's a bit choppy when the camera is at it's full distance.

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
avatar image
0

Answer by gpaulguilfoyle · Feb 04, 2020 at 10:34 PM

Check this out, it discusses camera smoothing, camera bounds and camera follow: unity 2d camera follow player tutorial

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Move an object in world space on the viewport 0 Answers

Platformer Flipping 0 Answers

following camara issue 1 Answer


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