Checking distance between 2 objects

 
Post new topic   Reply to topic    Unity Community Index // Scripting
View previous topic :: View next topic  
Author Message
cassius_b



Joined: 07 Nov 2009
Posts: 5

PostPosted: Sat Nov 07, 2009 10:08 am    Post subject: Checking distance between 2 objects Reply with quote
Hi.This is my first thread. How do I check the distance between the player controller and some other object in order to trigger off an event? Thanks.
Back to top
View user's profile Send private message
Eric5h5



Joined: 19 Jul 2006
Posts: 6930

PostPosted: Sat Nov 07, 2009 11:55 am    Post subject: Re: Checking distance between 2 objects Reply with quote
How about you just use a trigger? If you'd still rather use distance, use Vector3.Distance.

--Eric
Back to top
View user's profile Send private message Visit poster's website
.Amon.



Joined: 18 Oct 2009
Posts: 32
Location: London, England

PostPosted: Sat Nov 07, 2009 12:45 pm    Post subject: Reply with quote
Something like this?

XDistance = Xlocation1 - Xlocation2
YDistance = Ylocation1 - YLocation2
ZDistance = ZLocation1 - ZLocation2

Distance = SquareRoot(XDistance*XDistance + YDistance*YDistance + ZDistance*ZDistance)

_________________
http://www.kamikazekrow.com

May the sky never fall on your head.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Eric5h5



Joined: 19 Jul 2006
Posts: 6930

PostPosted: Sat Nov 07, 2009 2:32 pm    Post subject: Reply with quote
.Amon. wrote:
Something like this?

XDistance = Xlocation1 - Xlocation2
YDistance = Ylocation1 - YLocation2
ZDistance = ZLocation1 - ZLocation2

Distance = SquareRoot(XDistance*XDistance + YDistance*YDistance + ZDistance*ZDistance)


...Or you could just use Vector3.Distance. Smile

--Eric
Back to top
View user's profile Send private message Visit poster's website
.Amon.



Joined: 18 Oct 2009
Posts: 32
Location: London, England

PostPosted: Sat Nov 07, 2009 2:51 pm    Post subject: Reply with quote
True but being a Monkey with glasses i have to try to show that I know what's behind Vector3.Distance. Smile

I Iz teh l33t.

_________________
http://www.kamikazekrow.com

May the sky never fall on your head.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
defmech



Joined: 24 Feb 2007
Posts: 301

PostPosted: Sat Nov 07, 2009 5:02 pm    Post subject: Reply with quote
You can also use a raycast between the player controller/main camera and whatever object you're trying to measure. I definitely agree with Eric, though, Vector3.Distance is probably the simplest way to do it. I asked a similar question recently and at this point I wouldn't do it any other way.
Back to top
View user's profile Send private message
cassius_b



Joined: 07 Nov 2009
Posts: 5

PostPosted: Sun Nov 08, 2009 2:21 am    Post subject: Reply with quote
thanks all . i am new (yesterday) to this engine. Still feeling around.
Back to top
View user's profile Send private message
Omega



Joined: 31 Jul 2007
Posts: 120

PostPosted: Mon Nov 09, 2009 12:15 pm    Post subject: Reply with quote
I hope I'm not too late to offer something useful.

First, congrats on choosing Unity. I've never seen any other game development tool do what it can.

Second, this thread is technically in the wrong forum. For reference, something like this should go under "General" or "Scripting", instead of "Gossip". (since... you know... it's not something one is likely to go tell their friends about. Although you never know...)

Third, and on topic, finding the distance between two points can be done with, as mentioned, Vector3.distance. However, for what you want, you need to take a couple of things into account:

1) Finding square roots is computationally intensive. A simple way to avoid this is to use the Vector3.sqrMagnitude function and simply squaring the input. Huge performance boost right there, especially if you plan to use it in Update().

2) As was also suggested, you could use a trigger. From the docs:
Quote:
An alternative way of using Colliders is to mark them as a Trigger, just check the IsTrigger property checkbox in the Inspector. Triggers are effectively ignored by the physics engine, and have a unique set of three trigger messages that are sent out when a collision with a Trigger occurs. Triggers are useful for triggering other events in your game, like cutscenes, automatic door opening, displaying tutorial messages, etc. Use your imagination!

The messages that are "sent" (they work just like Update) are OnTriggerEnter, OnTriggerExit, and OnTriggerStay. Sounds like what you're after.

Hope it helps.

_________________
Use rock
to break glass
to get wrench
to break glass
to get rock
Back to top
View user's profile Send private message
Post new topic   Reply to topic    Unity Community Index // Scripting All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum