Reputation: 86
since im using rb.movepos it has no smoothing by drag so i tried to reuse the lerp code i have. here is the code:
if (HorizontalMov == 0 && VerticalMov == 0 || rb.transform.position.x == 0 && rb.transform.position.y == 0 && rb.transform.position.z == 0)
{
rb.transform.position = Vector3.Lerp(rb.velocity, new Vector3(0, rb.transform.position.y, 0), Time.deltaTime * slowDownSpeed);
}
if(HorizontalMov > 0 && VerticalMov > 0 || rb.transform.position.x > 0 && rb.transform.position.y > 0 && rb.transform.position.z > 0)
{
rb.transform.position = rb.transform.position;
}
and the problem is that at first my guy bounces up and down infinitely and can move then after the controls are let go he snaps back to the og position.
Link to video: https://streamable.com/jhpxe4
Upvotes: 1
Views: 1448
Reputation: 1720
If you want smoothing, you should use SmoothDamp()
If you want to keep using Lerp,
rb.transform.position = Vector3.Lerp(rb.transform.position, new Vector3(0, rb.transform.position.y, 0), t);
where t should be a value that goes from 0 to 1 and can be multiplied by speed. You can keep a class variable float startTime
and do float startTime = Time.time
at the start of the jump, so it will hold the time when jump started. And then t = Time.time - startTime.
If you're wondering, here is what lerp does. It takes 3 arguments, a, b, and t. Let's assume for simplicity that these are just floats, a = 0 and b = 10. In this case, Lerp() will return a value between a and b (0 and 10) based on t. If t is 0 or less than 0, it will return a which is 0. If t is 1 or more, it will return 10. If t is 0.5, it will return the value that is between a and b which is 5.
What this means is that for it to work accurately, you need to hold the value of a at the start of the lerp, not just give it position or velocity for the value of a.
Also you can't use Time.deltaTime, you have to use a value that starts from 0, like a float which you keep adding Time.deltaTime to, so it increase with time.
Upvotes: 1