So right now when it goes from one waypoint to another it works perfectly. However the distance between the waypoints are affecting the speed of the movement. If a waypoint is farther away form another, it will speed up drastically compared to close waypoints. How would I go about solving this?
Here is a webplayer (simply hover over a blue/green box and left click drag to move it. The faster you move your mouse the faster the box moves in that area of the line - so how would I make it a constant speed throughout the whole thing.
I like the way the webplayer demo acts.
I don’t get the stuff you do with “y” components only. Maybe you should elaborate a bit.
Anyway you need transform.Translate(testDirection*Time.deltaTime); as you already have normalized in the previous line.
Also I think it has to do with the density of the waypoints and the scale of the scene and boxes.
As you are normalizing and not using the Time.deltaTime you are making the box go 1 unit distance per Update, which translates to (for 30
FPS) 30 unit distances per second. So if each box is 1 units long, if the distance between waypoints is long enough it goes at this full speed.
As we lay the waypoints with the mouse, the distance between waypoints is very close unless we move the mouse very quickly. Lets say the mean distance between each waypoint is 0.2 units. The box tries to move at 1 units/update however as it meets the waypoint it halts motion so it appears to be moving slow, or it possibly overshoots and gets past its target waypoint and the next waypoint. And at the next update gets back to the target, moving back and forth so fast, and leading to a erractic movement.
If I do Time.deltaTime all it does is make the cube move extremely slow, but still doesn’t solve the fact that it moves different speeds. If you move the mouse quicker it will still move the box quicker as well, so time.deltatime does not fix this at all.
There is something else happening here that we cannot see. Other than the normalization of the frame speed, the code that you have would not produce the results that you are getting.
In tests: If I drew a line slow, it would move slow, if I drew it fast, it would move fast. The only thing I can think of is the magnitude check at the end. If your move was always greater then the magnitude then you would never reach the destination. (This would look like jittering at a point) This also is not happening.
Your translation line actually should look like this:
transform.Translate(moveDirection.normalized * speed * Time.deltaTime);
Perhaps there is something else that is producing your problem.