Comments and answers for "MoveTowards on paths, different speeds"
http://answers.unity.com/questions/1580667/movetowards-on-paths-different-speeds.html
The latest comments and answers for the question "MoveTowards on paths, different speeds"Comment by Emberheart-Games on Emberheart-Games's comment
http://answers.unity.com/comments/1581031/view.html
Not a problem at all! I'm just glad you got the solution and I hope this will help to you and others in the future :)Fri, 14 Dec 2018 12:02:08 GMTEmberheart-GamesComment by Wenon on Wenon's answer
http://answers.unity.com/comments/1580823/view.html
Hello, I didn't notice that I accidentally posted my problem twice. Somebody gave mi that solution:
[link text][1]
[1]: https://answers.unity.com/questions/1580666/movetowards-on-paths-different-speeds.html?childToView=1580673#answer-1580673
And it works, I believe your solution works too :) anyway, thank you for answer! And I copied your solution for future because I have a lot of plans about paths.Thu, 13 Dec 2018 17:29:07 GMTWenonAnswer 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