Answers for "MoveTowards on paths, different speeds"
http://answers.unity.com/questions/1580667/movetowards-on-paths-different-speeds.html
The latest answers for the question "MoveTowards on paths, different speeds"Answer by Emberheart-Games
http://answers.unity.com/answers/1580705/view.html
I'm not sure for what purposes you are using curves but from the first impression I think you should be using interpolation instead of MoveTowards because interpolation will make sure the point is between A & B consider time is in [0-1] range.
transform.position = Vector3.Lerp( p1, p2, time );
As you have figured it out there is a speed issue when it breaks on shorter paths etc. For this you need to set a consistent speed, imagine something like km/h.
float distance = ( p1 - p2 ).magnitude; // distance between current segment
speedFactor = ( 10.0f / distance ); // 10.0f is some custom factor
Speed factor makes sure the speed is consistent through the path. On short paths it will be bigger while on longer ones it will be smaller.
With speed factor calculated we can properly set the time which you need to feed the Vector3.Lerp method.
time += Time.deltaTime * speedFactor * speed;
if( time >= 1.0f )
{
time -= 1.0f;
SetNextPath();
}
transform.position = Vector3.Lerp( p1, p2, time );
![alt text][1]
That's rough explanation of solution to your problem. Here is a whole piece of code without the part where you get the next segment. I believe you have this figured out already.
public float moveSpeed = 1.0f;
private float speedFactor = 0.0f;
private Vector3 p1;
private Vector3 p2;
void Update() {
time += Time.deltaTime * speedFactor * speed;
if( time >= 1.0f )
{
time -= 1.0f;
SetNextPath();
}
transform.position = Vector3.Lerp( p1, p2, time );
}
// This method sets next segment between which object moves and calculates new speed factor
void SetNextPath() {
// get next segment of points (p1,p2)
float distance = ( p1 - p2 ).magnitude; // distance between current segment
speedFactor = ( 10.0f / distance ); // 10.0f is some custom factor
}
Here you go @Wenon. I'm not expert and there might be other, better solutions to your issue like Bézier curves etc. but this should still work in any case. Let me know how it goes!
[1]: /storage/temp/129384-preview.gifThu, 13 Dec 2018 10:59:56 GMTEmberheart-Games