- Home /
Lerping in One Direction
Hello,
First, my code:
hero.transform.position = Vector3.Lerp(hero.transform.position, Vector3.forward, Time.deltaTime);
What I want to do with this is move the GameObject from point A to point B. A simple transform.Translate WORKS, but Linear Interpolation is where it's at (especially when dealing with forces)
Using hero.transform.Translate(Vector3.forward * Time.deltaTime);
I can make the GameObject move exactly forward which is what I want to do.
When I try to Lerp it, however, I don't seem to go forward; I get thrown back, actually, regardless of the rotation of the GameObject.
Originally I thought the "smooth interpolation" from point A to B would cause this, but I've tried it both keeping the object's rotation constant and irregular. Both times I get stuck with this.
Any help would be great... or even a better understanding of what would need to be done to fix this. I just don't see why one would work very well while the other one would completely suck.
Answer by Professor Snake · Mar 10, 2013 at 05:05 AM
Vector3.forward equals to Vector3(0,0,1), which is not the position at which you want to have your gameobject. You could use (transform.position+transform.forward) to get a position that is exactly one unit in front of your object. (It might be transform.position-transform.forward , didn't get to test it)
$$anonymous$$y final code is :
hero.transform.position = Vector3.Lerp(hero.transform.position, hero.transform.position + hero.transform.forward, Time.deltaTime * 10);
Works just as intended. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613094133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Mathf.LerpAngle to gradually rotate an objects x position 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers