- Home /
Make object go 2 steps forward in period of time
I know this sounds like a question which has been asked before. But i cant find any answers on the net, so know im asking! How do i make my object move 2 on the z-axis over a period of time if i press space?
Answer by allenallenallen · Jul 16, 2015 at 02:59 PM
You just didn't search enough. http://answers.unity3d.com/questions/296347/move-transform-to-target-in-x-seconds.html
Vector3 startPos;
Vector3 endPos;
float timer = 0;
float seconds = 8; // The total time you want the movement to take.
void Start()
{
// Initialize startPos and endPos here.
startPos = new Vector3(0, 0, 0);
endPos = new Vector3(startPos.x, startPos.y, startPos.z + 2);
}
void Update()
{
timer += Time.deltaTime / seconds;
transform.position = Vector3.Lerp(startPos, endPos, timer);
}
Now this will move the object as soon as you run the game. You can easily modify it so that it moves only when the space is pressed. Or you can make it so that it will move to the endPos once you press space. I don't know how you want it to be so I leave that to you.
Your answer
Follow this Question
Related Questions
weapon bobbing 1 Answer
Animation play 1 Answer
How To Slow Camera Position Transform? 3 Answers
What is the best way to assign a duration to particles? 1 Answer