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