unity get distance between two objects 1

unity get distance between two objects

// Vector3.Distance is the same as (a-b).magnitude

float distance = Vector3.Distance(a.transform.position, b.transform.position);

Here is what the above code is Doing:
1. We’re creating a variable called distance.
2. We’re setting the value of distance to the distance between a and b.
3. We’re using the Vector3.Distance function to calculate the distance between a and b.
4. We’re using the transform.position property to get the position of a and b.

Similar Posts