- Home /
Best way to have GameObjects reach various targets at the same time
I am currently trying to create a turn based game with some soft real-time concepts and would like all object to move to their next position the same time that the player moves. I am also trying to figure out an algorithm of trying to make sure that all NPCs reach their destination at the same time as the player. For example given this small representation of the game world
P-----O-------[]------------------E
The player, P
, is currently 5 units away from the selected position of O
, but the enemy, E
, is 20 units away from []
. I was thinking of trying to get the distance of P->O
and E->[]
and applying some sort of speed modifier to each object's movement, but wanted to know if Unity had some sort of way to help simplify this so that all movement can start and end at the same time.
Answer by myzzie · Aug 24, 2020 at 03:30 AM
This can be achieved by linear interpolation
Origin > Target in t time
float t = 0;
while(t < 1)
{
t += timeToReachTarget / Time.deltaTime;
}
//Everybody that moved from origin position to target position by t will reach their destination at the same time.
position = Mathf.Lerp(from, to, t);