I can think of lots of ways to do it… what’s the best?
I want my object’s scrip to know when it enters a space–as defined by an invisible cube or sphere, say.
Like collision detection without bouncing off…
I can think of lots of ways to do it… what’s the best?
I want my object’s scrip to know when it enters a space–as defined by an invisible cube or sphere, say.
Like collision detection without bouncing off…
I’d say use a trigger, and have a tiny script on the trigger with an OnTriggerEnter function, which just does a SendMessage to a collider that enters the trigger. Maybe pass the name of the trigger if you need specific info like that.
–Eric
That makes sense to me. It would be great to have a way for an object to know it had entered a trigger, without having to put a script on the trigger itself. Like checking the trigger’s name.?
I was just using a distance comparison, and that works fine (for spheres) but it’s not flexible and probably a bad habit! So I wanted to learn what I SHOULD be doing before I forgot to ask
The object entering the trigger also gets the message. The only catch is that one of the two objects has to be a rigidbody for all of these trigger (and collision) messages.
http://unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html
-Jon
I’m guessing a distance comparison using Vector3.Distance is more efficient than using triggers as long as a spherical/circular boundary is all you need.
Since that takes, like, virtually no effort and has basically no effect on speed, I can’t imagine why. However, aarku is right, the object entering the trigger also gets the message, which I knew but totally forgot about… I would expect that scripting distance comparisons yourself is going to be slower than triggers, since those are done 100% natively and while user compiled code is relatively fast, it’s apparently 50% as fast as native code. But probably not enough difference to be noticeable.
–Eric
I just like having fewer scripts, and more in one script
But now I have some good things to think about!
I totally understand that since I felt that way myself ;), but I’m starting to think it’s more of a bad habit in Unity than anything, and that it’s better to have a lot of little modular scripts than one colossal do-all script that becomes hard to maintain.
–Eric
You haven’t seen my Director work :lol:
If you don’t like the trigger approach for some reason, which has the smallest performance overhead btw.
You can use: Physics.OverlapSphere which gives you back a list of colliders.
Neat–I will keep that method in mind too!