- Home /
Vector3 Lerp using Time.deltaTime?
Hello guys, i'm making my own character controller and physics, and currently, the final line of my script is just like this:
myTransform.position = Vector3.Lerp(myTransform.position,(myTransform.position + (myTransform.rotation * finalmovement)),0.8);
that finalmovement is a Vector3 that i get after calculating the physics and player inputs, to see where the character is gonna move. The problem comes in the last part of the line, where i have to put the "time". Currently, i'm not usign Time.deltaTime, and its working fine, if I creaty a build and play it, the movement is smooth, and even if i test in other PCs, the movement continues the same. That's nice. In the editor mode, however, its not smooth and sometimes its gets very laggy. That's bad, because I have to test everything via build & run.
I tried using Time.deltaTime, but the movement differs greatly from the editor and the build. using for exemple, 40 * Time.deltaTime, in the editor, the character moves very fast, but in the build, it barely moves.
Can someone explain me why this happens? I tought using Time.deltaTime would give the same results, independent of the frame rate ( that is slower in the editor ).
And wich one is best when making this kind of precise movement?
Thanks in advance!
EDIT Acutally, i do use Time.deltaTime in my code, but not on the Lerp itself I use time.deltaTime to "find" the values, something like this: finalmovement.y -= gravity * Time.deltaTime
and after that i do the lerp without Time.deltaTime
does will guarantee me that the speed is the same in every computer?
Answer by W.Walter · Dec 15, 2013 at 12:43 AM
I always use this with Time.delta and have never had any problems. Yes it will be dodgy without it because the speed will depend on the frame rate.
myTransform.position = Vector3.Lerp(myTransform.position,(myTransform.position + (myTransform.rotation finalmovement)),something Time.deltaTime);
Your problem will be something else in your code.
So you're doing it wrong too. Lerp abuse is pandemic in Unity noobs. What you really want when you think you want Lerp is SmoothDamp, and that already takes Time.deltaTime into account.
Answer by infinitypbr · Dec 15, 2013 at 12:44 AM
I have a few suggestions, since I think I'd need to see more of your code to really know what's what.
Time.deltaTime is immensely important. If you have a code that moves your character forward and you want the speed to be 5 units per second, the only way to make that work on all builds is to multiply it by Time.deltaTime so it only moves the appropriate amount each frame.
Try using the built-in controllers. Rebuilding the wheel is usually a bad idea and will cause problems. Once you use the basic ones that are built in, try manipulating/customizing it from there.
Actually, I've already spent a lot of time studying these controllers, also, lot of time praticing with my own :)
I forgot to mention on the "awser", but I'm usign Time.deltaTime to insert the values on the finalmovement Vector3. Something like:
finalmovement.y -= gravity * Time.deltaTime
i use time.deltaTime to set the values, but not on the lerp, does that gives me guarantee that the movement will be the same in every build? $$anonymous$$orrow i'll test in another PCs to see if i get the same results, if so, i'll stick with it.
I suppose I'm getting confused as to why you're using Lerp for movement, but, aside from that, I think you could get funky results doing the Time.deltaTime computation too early.
Answer by Kiloblargh · Dec 15, 2013 at 08:02 AM
You are mistaking Lerp for SmoothDamp. The two are not interchangeable.
Your answer
Follow this Question
Related Questions
Stopping Vector3.Lerp 3 Answers
Vector3.Lerp doesn't work on build 0 Answers
Smooth movement like Lerp with Touch Input? 2 Answers
Object movement with smooth start and end? 2 Answers
3 coroutines to ease in lerp, MoveToward steady pace, and ease out lerp 0 Answers