# How to move the object with same pace regardless their target distance?

so I tried to get those first and third ball to be able to move with same pace no matter how curvy their target path is, I tried to get the y position of the target that the ball is moving to, but I cannot figure out how to actually control the speed of the ball when the target y position is higher or lower than the ball y position, it seems that the ball will keep on speeding and not at the same pace with other ball as well.

Here is the code so far..

float yDistance = transform.position.y - targetWayPoint.transform.position.y;

if(yDistance != 0)
{
speed = speed + 0.5f;
}
transform.position = Vector3.MoveTowards(transform.position, targetWayPoint.position,  speed*Time.deltaTime);

if(transform.position == targetWayPoint.position)
{
currentWayPoint ++ ;
targetWayPoint = wayPointList[currentWayPoint];
}


Any ideas? Thanks alot!

• I am unclear what you mean by "same pace"? Do you mean same horizontal velocity? So they traverse the whole path in the same time? Because if you want them to move at the same actual speed there is no need to modify the speed when the path differs. – Jack Aidley Oct 15 '18 at 11:27

I assume that the code posted is in some update function? It appears as if you are continuously increasing the speed if you increase the y distance. Doesn't seem like that is necessary.

What you want to do is calculate the distance between each waypoint, and then adjust the speed so the time it takes to reach the next waypoint is the same.

First, decide how long you want it to take the sphere to travel between waypoints. Let's say you want 1 second, so...

travel_time = 1.0;


Next, assume you are at a given waypoint, and want to figure out how fast to go to the next waypoint. Start by calculating the distance to the next waypoint...

distance = calculate_distance(current_waypoint.position, next_waypoint.position);


Now given you have a distance, you can calculate what speed to use...

speed = distance / travel_time;


So if you have a distance of 1 for example, the speed is 1 / 1 = 1. But if the distance was 2 (twice as far), the speed is 2 / 1 = 2 - meaning you have to travel twice as fast to cover the distance in the same time.

If you recalculate this speed at each waypoint, that will make the spheres all travel across the lines in the same amount of time, meaning they will both reach the far side at the same time.

Your code has a couple of errors in it I wanted to point out. Fixing these won't get you the desired outcome, but I thought it would be important to point them out for your education.

First, the problem with your current code is that when you grab and move the waypoint upwards, you're triggering the following bit of code...

if(yDistance != 0)
{
speed = speed + 0.5f;
}


So as long as your waypoint is moved up, the speed will just continue to increment more and more, making the sphere move faster and faster. It will just keep getting faster and faster until you move it back down to zero.

Second, I think this test is not doing what you think it should...

if(transform.position == targetWayPoint.position)
{
currentWayPoint ++ ;
targetWayPoint = wayPointList[currentWayPoint];
}


You are trying to compare the two positions to determine if the current position is the same as the target waypoint. But unless the positions are EXACTLY the same, this won't work. So if the position goes just slightly past the waypoint, then they won't be equal - but clearly the sphere will have reached the waypoint. Instead you'd want to do something simple like check if the sphere X value was > the target waypoint X value.

• Your solution is working and all of my spheres are moving at the same pace ty, however, I did this if(transform.position == targetWayPoint.position) to check later on Updates() if the currentWayPoint is larger than the array length so I can reset everything and loop back the spheres movement. Unless you have some other ways though... – Mira Oct 16 '18 at 4:19