Calculate time to destination taking account current speed, also how to determine distance between two objects?
As the title suggest I need little help on how would I calculate a distance between two objects, which will probably end up being really simple. And then how to calculate how much time will it take to reach a certain destination and no I'm not using rigidbodies just transform that is being moved with different speeds so how do I calculate the time it will change and change it depending on the change of speed?
Comment
Best Answer
Answer by MT369MT · Aug 13, 2018 at 06:12 PM
Hi, I created this script that calculates the Distance between two objects (That i called Object1 and Object2) using the Vector3.Distance function. To know the Time to reach the object (supposing that only Object1 will move) simply use the formula: Time = Distance / Speed.
public GameObject Object1;
public GameObject Object2;
public float Distance;
public float TimeBetweenObjects;
public float Speed;
void Update()
{
Distance = Vector3.Distance(Object1.transform.position, Object2.transform.position);
TimeBetweenObjects = Distance / Speed;
Object1.transform.position = Vector3.MoveTowards(Object1.transform.position, Object2.transform.position, Speed * Time.deltaTime);
}