I cant figure out what is wrong with my code
float time = 0;
while(time <= 1)
{
ball.position = Vector3.MoveTowards(ball.position, this.transform.position, time);
time += Time.deltaTime * 0.1f;
}
This is my script code and I want to make 'ball.position' moves to 'this.transform.position' smoothly!!
but every time when i try this code, the ball always teleports to 'this.transform.position'
Can somebody help me figure out what's wrong with this script
Answer by PhilippCh · Jul 29, 2016 at 12:36 PM
Your movement is all happening in the same Update() cycle. Basically, it updates the values all the way to Time >= 1 in one frame, then renders everything (the sudden jump you see) and is done.
To correct that, you need to remove the while loop and (if you haven't done so already) move your code within the Update() or LateUpdate() functions.
Also, with questions like this, be sure to always read the documentation on the function you're trying to access. In this case, Unity's documentation has a perfectly valid example of what you're trying to achieve.
Your answer
Follow this Question
Related Questions
Cant Move towards Right and Jump at same time? 2 Answers
GameObject doesn't move 1 Answer
Stop movement on collision 1 Answer