# Distance between two objects?

Hello,

I need to write a script that tells the player how far away object 1 is from object 2.

But I’m having difficulty. Could someone help point me in the right direction please

Thanks a lot,
Stuart

1 Like

In C#

``````float distance = Vector3.Distance (object1.transform.position, object2.transform.position);
``````
16 Likes

Yep, here’s how that works internally (probably): You find the difference between the two vectors (v1, v2) by subtracting them from one one another and storing the result in a third vector:

``````Vector3 difference = new Vector3(
v1.x - v2.x,
v1.y - v2.y,
v1.z - v2.z);
``````

The distance between the two vectors is this vector’s length (or ‘magnitude’, a property which Unity handily provides for you), which you could manually calculate by using some trigonometry:

``````float distance = Math.Sqrt(
Math.Pow(difference.x, 2f) +
Math.Pow(difference.y, 2f) +
Math.Pow(difference.z, 2f));
``````
9 Likes

Thanks a lot for your help. I get the basic idea.
But I should of said earlier that I’d only really understand javascript

1 Like

Hmm…

I still can’t get it to work.

Could someone write a script for me in Javascript?
I also want the value to be displayed as a Gui.

Thanks,
Stuart

Try this, just a small test but maybe it will help.

244546–8800–\$dist_121.rar (836 KB)

Thanks! That worked perfectly. I just found this in the script reference with a perfect example as well:

I probably should’ve looked harder

Now I just need to display the speed my object is traveling which so far seems to be much harder :roll: and I haven’t even started to think about converting it to Km/h

I am not very comfortable with JavaScript, but I hope you understand the idea.

``````private var previousPosition: Vector3;
private var speed: float;

function Speed () {
return (speed);
}

function SpeedInKmH () {
return (speed * 3.6);
}

function Start () {
previsousPosition = transform.position;
}

function Update () {
var distance: float;
distance = Vector3.Distance (previousPosition, transform.position);
speed = distance / Time.deltaTime;
previousPosition = transform.position;
}
``````
1 Like

give me a break. why not just give a simple answer dude. the most complex coders in the world will always use the EASIEST code.

why isnt the code more versatile, for example:

why cant i just name 2 objects
lets say i have 2 objects named object1 and object2
then just use:

if (Vector3.Distance(object1,object2) < 100) {}
im finding i have to write complex scripts just to do a simple thing like find a distance between 2 objects?

the other game engines i have worked with make this much easier.

2 Likes

It’s always a good idea to pay attention to dates when posting on forums. Trying to start an argument over a 2-year-old post isn’t the best use of your time.

You can. That is in fact what you do, though you’d need to specify “.position”. Vector3.Distance(object1.position, object2.position)

You don’t have to write complex scripts, and I seriously doubt you can name any game engines where it’s any easier, considering that it’s a trivial 1-line command in Unity. Probably best to make sure you actually understand what it is you’re doing before making unfounded complaints.

–Eric

8 Likes

thanks guys. sorry for my attitude i have figured out the paradigm. here is a video from the game i am currently designing. im having to generate artificial gravity for certain scenarios so you can see things start to get complex.

figuring out new things makes me angry and irrate, but i just keep trudging.

here is a video:

im am currently learning the unity engine from the floor up. you will be seeing more of me on these threads soon.

Gooncorp

1 Like

var speed : float;
var speedGUI : GUIText;

function Update(){
speed = rigidbody.velocity.magnitude;
speedGUI.text = speed.ToString();
}

if i remember right…

I wrote a simple editor script that does this. For those whom still end up in this tread. (Like myself ;P)

NOTE: I created it for unity 5.6

3124973–236672–Editor Ruler.unitypackage (934 Bytes)

hello. can we make distance for three object? thanks

Do you mean like if you wanted to add them together or something? Explain what you mean

I haven’t seen much that can’t be done in programming, it just can get complicated. I also don’t know why you would need that, the function gives the distance between 2 points in space, a third point would mean extra distances between point A and B, A and C, and B and C. It’s a two-point function, it’s like asking ‘if you can add a third vector to a 2D vector?’, and the answer’s no, it’s a 2D vector with only 2 vectors(and Distance is a ‘2D’ function, Pos1 and Pos2). You could make whatever custom function you wanted to compare 3 points in space so have fun.

i get this page might be dead but how do i get the distance from one object to multiple so for example say i have the main character and to attack the closes one it needs to check the distance from 4 enemies(min at the moment) and select the closest enemy to it how would i do that as using vector3.distance gives me a overload error

You have to check all 4 and find the lowest value.

Cheers