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
0
Question by Jabbasmakt · Dec 18, 2021 at 02:46 AM · rigidbodypositionvelocityjitterinterpolation

Get Interpolated rigidbody velocity

Hello fellow creators! I'm trying to get the interpolated velocity of a RigidBody to use when updating the camera lerp speed. This is because I want the camera to speed up the faster the player is going so that it doesn't lag far enough behind the player. My implementation introduces some jitter even though I use interpolated RigidBodys and use the transform position, which should give me the interpolated positions to calculate the velocity.

And I don't want to put the camera logic in FixedUpdate(). I want the camera to be as smooth as possible on a high refresh rate monitor. Here is the code:

 LateUpdate(){
      //using transform (not rigidbody) to get distance/velocity between last frame.
      float velocity = Vector3.Distance(playerPos.position, lastPosition)/Time.deltaTime; 
 
      lastPosition = playerPos.position; 

      //Lerp the camera position using the speed of velocity.
      camera.position = Vector3.Lerp(camera.position, goalPos,velocity*Time.deltaTime); 
 }
   

Thank you for your time!

Comment
Add comment · Show 4
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 DenisIsDenis · Dec 19, 2021 at 04:26 AM 0
Share

Hello, in your code, you find velocity and divide it by Time.deltaTime. And when you start using it, you multiply it by Time.deltaTime. Therefore, these two actions do not make sense.

⠀

Why not just use Vector3.Lerp without finding the velocity? And so that the camera cannot lag far behind the player, it is only necessary to add a condition for checking the distance. The code:

 void LateUpdate()
     {
         camera.position = Vector3.Lerp(camera.position, goalPos, Time.deltaTime * 5);
 
         float distance = 2;
         if (Vector3.Distance(camera.position, goalPos) > distance)
         {
             camera.position = goalPos + (camera.position - goalPos).normalized * distance;
         }
     }
avatar image Jabbasmakt DenisIsDenis · Dec 19, 2021 at 11:24 PM 1
Share

Thank you for your response! You're right that Time.deltaTime should only be used once on velocity. For some reason I thought that it had to be done again when lerping since I'm so used to always multiply that value with deltaTime. Though this is only necessary when putting in a static number such as in your case 5.

The reason why I want to use velocity to control the speed of the lerp is so that I can get more control over the movement of the camera. Then I could have a second slower lerp on the velocity to make the camera appear to first lag behind the player and then recover and zoom back in slowly. I'm working on a racing game so the sensation of speed is really important.

Right now though, even when removing one of the Time.deltaTime multiplications, there is still some jitter when using the velocity of the player. Maybe I'll be trying to convert either the camera to be an interpolated rigidbody, or I'll try to convert the player to not use a rigidbody and instead use a simple collider and program the movement of the vehicle myself.

But thank you once again. I'll be testing a few other things.

avatar image xxmariofer Jabbasmakt · Dec 20, 2021 at 10:26 PM 0
Share

You should not be using Lerp (at least like that) , and if used, remove both time.deltatime, they are both wrongly used and that's one of the causes of the jitter. Use move toward with animation curves if you want full control of thee camera position.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by xxmariofer · Dec 21, 2021 at 09:16 AM

You could be using Lerp, but not like that, you will see many comments suggesting to use lerp like that, they are all wrong. dont use Time.deltaTime with Lerp, it doesnt work, you wont be able to manage to use Lerp framerate independant like that. For me to try to explain why not to use Lerp like that you would need to really understand how Lerp works first. Unity has no smoothing functionality build into the editor, you would have to code it yourself. The best approach is to use MoveTowards with animation curves, is there any reason you would prefer not to use them?

here Ill share a simple example of why Lerp doesnt work with timedeltatime:

Imagine your start pooint is 0,0 and your target is 1,0 ill use 2 examples, one running at 1 fps and other 5 fps and your "velocity" will be 0.5 * time.deltaTime

 second 0     second 0.2     second 0.4     second 0.6        second 0.8       second 1.0
 
 Ex1 (0,0)         (0,0)          (0,0)         (0,0)           (0,0)            (0.5,0) 
  
 Ex2 (0,0)         (0.1,0)       (0.19)     (0.271, 0)...........            (~0.43, 0)

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

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

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

Take final position after a force applied to a GameObject 1 Answer

How can I increase or decrease a position based on another variable ? 1 Answer

Is my (LateUpdate) camera + rigidbody causing jitter? 2 Answers

Problem stopping rigid body after adding a force 0 Answers

Velocity powered rigidbody on a moving platform without parenting. 3 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