- Home /
Calculate distance between two objects
How can i calculate the distance between two objects in unity using javascript?
Answer by Eric5h5 · Mar 17, 2010 at 09:49 PM
var distance = Vector3.Distance(object1.transform.position, object2.transform.position);
For those who want C#
public float distance; // distance between enemies and players public Transform target;
void Update()
{
distance =Vector3.Distance(transform.position,target.position);
}
I clearly don't understand how this works because it isn't working properly with my game and I don't know why:
I have this script attached to my player character:
public class IsPlayerNextToDoor : $$anonymous$$onoBehaviour
{
public Transform door;
float distance;
public float maxAllowedDistance = 3f;
private void Update()
{
distance = Vector3.Distance(transform.position, door.position);
if (distance < maxAllowedDistance)
{
isPlayerCloseToDoor = true;
print("Player is close to door");
}
else
{
isPlayerCloseToDoor = false;
print("Player is too far from door");
}
}
}
And I've dragged the door prefab into the appropriate box in the Inspector for the IsPlayerNextToDoor script attached to the player.
But when I go to check this in my game, where I have three instances of my door prefab, it only works on one of them and then only once at that, and the debug text never changes again after the first time when it confirms I'm next to the one particular door in the game it works on.
$$anonymous$$y thinking is that the debug text should keep switch between the two states/sentences as I move in and out of range of the door, and that this code should work on every door I approach too, but it's obviously not doing quite what I expected it to.
Any idea what might be up here?
Is there maybe something silly I might have done that would make this presumably otherwise correct code not work as intended in my case specifically?
For me, it says:
'position' is not a member of 'UnityEngine.Vector3'.
I have a variable in there that is currently empty but will be filled in-game with a rigidbody.
Answer by efge · Mar 17, 2010 at 09:52 PM
Look at the reference for Vector3.sqrMagnitude (with example).
Answer by Zootie · May 05, 2010 at 07:21 PM
There is also Vector3.magnitude...
but what is the difference between using Vector3.Distance(a,b) and Vector3.magnitude(a-b)? Does one compute faster than the other?
It'd be (a-b).magnitude for the latter one - which involves creating a new vector3 just for finding the magnitude, which Distance skips. $$anonymous$$agnitude is great for calculating the length of a single vector though
Answer by sandhillceltic · Jun 25, 2013 at 03:49 PM
A more mathematical approach, you could use the Pythagorean Theorem and calculate the hypotenuse using the right triangle with the grid units in unity. So, the square root of (delta)X^2 + (delta)Z^2 = flatdistance (this is a var) and then... square root of flatdistance^2 + (delta)Y^2 = distance between the two points
What @sandhillceltic said, plus the advice that the transform attached to each gameobject has a position member, which you'll need.
Answer by Verontrax · Jul 15, 2016 at 05:38 AM
My suggestion is to avoid taking the square root of anything.
I would do this buy finding the distance of characters x and y position from target and squaring each one and add them together.
(Yc - Ye)^2 + (Xc - Xe)^2 = D^2 , Where Xc and Yc are your characters position. amd Ye, Xe is the enemies and/or targets position.
for comparing it if it is in range, use range of whatever R, display it as R, but internally it should be stored as R^2 to compare to D^2. Thus avoiding any complicated math.
if you are using 3d graph you need to ad deltaZ^2 to D^2 as well (delataZ = Zc - Ze)
Why do I suggest not using the square root, first. you can use the distance squared just as easily as the you could use the actual distance and taking the square root of a number is slow... I mean it only takes around O(log n) (or better depending on what method is used) but to do better you need complicated math which might not actually make it any faster (depending on the processor being used) where as keeping the distance as itself squared gets rid of an unnecessary object and takes O(1) time :)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Move a Person to a point / Repeat a function called from another script? 1 Answer
Display the speed of a Game Object in MPH 1 Answer
"Can't find namespace" - accessing Javascript from C# (or C# from Javascript) 3 Answers
Javascript Numerical Variable Types for Working With Lidgren 2 Answers