| View previous topic :: View next topic |
cassius_b
Joined: 07 Nov 2009 Posts: 5
|
Posted: Sat Nov 07, 2009 10:08 am Post subject: Checking distance between 2 objects |
|
|
|
| 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 |
|
|
Eric5h5
Joined: 19 Jul 2006 Posts: 6930
|
Posted: Sat Nov 07, 2009 11:55 am Post subject: Re: Checking distance between 2 objects |
|
|
|
How about you just use a trigger? If you'd still rather use distance, use Vector3.Distance.
--Eric |
|
| Back to top |
|
|
.Amon.

Joined: 18 Oct 2009 Posts: 32 Location: London, England
|
Posted: Sat Nov 07, 2009 12:45 pm Post subject: |
|
|
|
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 |
|
|
Eric5h5
Joined: 19 Jul 2006 Posts: 6930
|
Posted: Sat Nov 07, 2009 2:32 pm Post subject: |
|
|
|
| .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.
--Eric |
|
| Back to top |
|
|
.Amon.

Joined: 18 Oct 2009 Posts: 32 Location: London, England
|
Posted: Sat Nov 07, 2009 2:51 pm Post subject: |
|
|
|
True but being a Monkey with glasses i have to try to show that I know what's behind Vector3.Distance.
I Iz teh l33t. _________________ http://www.kamikazekrow.com
May the sky never fall on your head. |
|
| Back to top |
|
|
defmech
Joined: 24 Feb 2007 Posts: 301
|
Posted: Sat Nov 07, 2009 5:02 pm Post subject: |
|
|
|
| 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 |
|
|
cassius_b
Joined: 07 Nov 2009 Posts: 5
|
Posted: Sun Nov 08, 2009 2:21 am Post subject: |
|
|
|
| thanks all . i am new (yesterday) to this engine. Still feeling around. |
|
| Back to top |
|
|
Omega

Joined: 31 Jul 2007 Posts: 120
|
Posted: Mon Nov 09, 2009 12:15 pm Post subject: |
|
|
|
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 |
|
|