- Home /
How to calculate Distances
Hi,
I really am sorry for the noobish nature of this question. I further apologize for essentially asking for code. What I am wondering is how can I calculate the distances between two tagged gameObjects such as "Player" and "enemy". I am in need of this so that I can more precisely know the distance between the "Player" and the "enemy" so that I can better set up animations for fighting and what not.
Thank you for your assistance.
Answer by temptest123 · Jan 23, 2013 at 04:12 AM
enemyDistance = Vector3.Distance( GameObject.FindWithTag ("enemy").transform.position, GameObject.FindWithTag ("Player").transform.position);
Thank you so much! That worked exactly as I needed it to.
Answer by Dave-Carlile · Jan 23, 2013 at 03:16 AM
You can use Vector3.Distance to determine the distance between two objects. You can identify the two objects by tag by using GameObject.FindWithTag.
that much I totally understand, I am wondering how to actually write it. Let's call this concept "enemyDistance". Would it be written like this:
enemyDistance = Vector3.Distance( GameObject.FindWithTag ("enemy"), GameObject.FindWithTag ("Player"));
Sorry, I am just not sure about how I deter$$anonymous$$e this "enemyDistance" value. I assume that taking the distance between the "enemy" and the "Player" will return the value for "enemyDistance".
Does that make sense?
Your answer
![](https://koobas.hobune.stream/wayback/20220613091437im_/https://answers.unity.com/themes/thub/images/avi.jpg)