Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Kaji-Atsushi · Nov 25, 2013 at 11:38 PM · camera2dscreenfollow

How to 2D Camera follow with lag.

I've tried to get this code working, I know it should be simple, but apparently I can't wrap my head around it. So any guidance would be nice.

My situation is kind of like a infinite runner/2d platformer and the character is rigidbody2D driven. Basically what I'm trying to do is have the camera follow the Character but with a lag time of catching up... For example, let's say the character is running at a constant speed of 5. During this time the character would be at the same position in the camera view and only the environment would look like their moving. Now the character accelerates to a speed of 10, I want the character to move forward relative to the character in screen position and a few seconds later the camera would catch up to the character going at the speed of 10 and bring him back to the original character screen position.

Thanks for your time.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by PhilRousse · Nov 26, 2013 at 12:04 AM

Animating the Camera

o achieve what you want, you need to animate the camera position. You'll find next a code example and explanation of this code.

What the code do

n this code, we assume that the camera is a children of the character that you are following.

When we call the StartLag function we're telling the code to start animating an offset on the camera position. The animation will take lagDuration (in this example, 2 seconde) to play the lag. The camara will be move base on the offset vector (-5 unit back on the X axis). ## SmoothStep ## For animating, we use the `Mathf.SmoothStep` function. Here the description:

This function interpolates between min and max in a similar way to Lerp. However, the interpolation will gradually speed up from the start and slow down toward the end. This is useful for creating natural-looking animation, fading and other transitions.

We'll use that function 2 time:

  1. FROM original position TO offset position

  2. FROM offset position TO original position

    lagPeak

    ou'll probably want to reach the offset position quicker than it take to come back to the original position. lagPeak allow you that. To calculate the time it take to reach the peak, multiply lagPeak and lagDuration. Here an example of what it mean:

  • If lagDuration == 2f and lagPeak == 0.5f, the camera will reach is offset in 1 second and come back in 1 second.

  • If lagDuration == 2f and lagPeak == 0.1f, the camera will reach is offset in 0.2 second and come back in 1.8 second.

    Code Example

    //Public Interface. Tweak these value to change the result. [Range (0,1)] public float lagPeak = 0.5f; public float lagDuration = 2.0f; public Vector3 offset = new Vector3(-5,0,0);

    //Internal variable (not to modify) private float movementTimeStart = 0.0f; private bool isLagin = false

    ///Initialize Lag animation value void StartLag() {

       isLagin = true;
         movementTimeStart = Time.time;
         Vector3 camInitialPosition = this.transform.localPosition
         Vector3 camOffsetPosition = camInitialPosition - offset;
     }
     
     void Update()
     {
         AnimateLag();
     }
     
     ///Animate the Lag
     void AnimateLag()
     {
         float t;
         //If the animation duration is invalid, jump to the end. (Prevent dividing by 0)
         if (lagDuration <= 0.0f)
         {
             t = 1;
         }
         else
         {
             //Calculate the pourcentage of the animation
             t = (Time.time - movementTimeStart) / lagDuration;
         }
         
         //Animate the piece
         //
         // The animation is separete in 2 part, Reaching the peak and comming back.
         // To adjust the peak timing, we use the lagPeak variable that represent when (in %) the peak should be reach
         if(t < lagPeak)
         {
             this.transform.localPosition = new Vector3 (Mathf.SmoothStep (camInitialPosition.x, camOffsetPosition.x, (t * (1/lagPeak))),
                                                         Mathf.SmoothStep (camInitialPosition.y, camOffsetPosition.y, (t * (1/lagPeak))),
                                                         Mathf.SmoothStep (camInitialPosition.z, camOffsetPosition.z, (t * (1/lagPeak)));
         }
         else
         {
             this.transform.localPosition = new Vector3 (Mathf.SmoothStep (camOffsetPosition.x, camInitialPosition.x, (t * (1/(lagPeak-1)))),
                                                         Mathf.SmoothStep (camOffsetPosition.y, camInitialPosition.y, (t * (1/(lagPeak-1)))),
                                                         Mathf.SmoothStep (camOffsetPosition.z, camInitialPosition.z, (t * (1/(lagPeak-1))));
         }
         
         //If we have finish
         if (t >= 1)
         {
             //Reset Animation data
             isMoving = false;
             movementTimeStart = 0.0f;
             camInitialPosition = Vector2.zero;
             camOffsetPosition = Vector2.zero;
             lagDuration = 0.0f;
         }
     }
    
    
    

For more code explaination, just comment.

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 Kaji-Atsushi · Nov 29, 2013 at 09:53 AM 0
Share

I typically don't do this but...I've no idea how this code works. So some code explanation would be nice. Typically I code in US ins$$anonymous$$d of C#, so if you happen to be know US that would be nice if you could convert it for me. Though I've had to convert several C# scripts before and haven't ran into many problems till now... Thanks for your help!

avatar image PhilRousse · Nov 29, 2013 at 03:18 PM 0
Share

I've add more explanation on what the code do. If you want me to explain code line, please ask specificly.

For code convertion, I don't know what US is. Do you mean JS like in Javascript?

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

17 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

Related Questions

Polygons clipping at edge of screen 1 Answer

2D Game. Screen, Camera and coordinates. 0 Answers

How to letterbox the screen and have pixel perfection in a 2d game 0 Answers

Smooth Camera 2D Follow Player 0 Answers

Set limits for camera to other axis 2D 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