- Home /
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!
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;
}
}
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.
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.
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)
Your answer
Follow this Question
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