- Home /
Simple Object Collision (again)
Hi,
I have a rather simple collision problem, but I can't get it working. I have read the manual about physics and all the answers in here about collisions and detection, but for me it doesn't work.
I have the following scenario (simple Space-Invaders-Clone). One Prefab (Enemy) with an attached Script, that handles the "OnTriggerEnter"-Event. A box-collider attached to this object with "Trigger" checked. Another Prefab (Bullet) also has an Script attached and a box-collider. Both Objects are moving and should collide, but the event doesn't get called (I have a debug.log-call inside the method).
I'm still confused if I have to use a rigid-body for this kind of simple collision detection, I absolutely don't want any physics simulation. Plus I don't know if the collision-box moves with the gameobject (it should, because it is a child of it, I think).
I'm really not a novice programmer, but new to unity. Am I missing something simple or is it really a hassle to get simple collisions?
Please don't refere to the documentation or specification, I read them already twice :).
Other solutions to this problem are welcome.
regards
knoggly
Answer by Shawn · Feb 22, 2010 at 06:20 PM
In order for 2 colliders to collide one of the colliders must have a rigid body assigned. You can tweak the settings on this rigidbody so it does not effect the gameObject by turning off gravity etc... but in order for the collision event to happen one of your bodies needs a rigidBody.
I did so, but it doesn't change the result. Is the order of the components important or has the box-collider some special behavior or setting I'm not aware of?
regards
The order is not important but you need to make sure that the script with which you are attempting to capture the OnTrigger events with is on the same GameObject as the collider. It must be on that specific GameObject, not a parent object.
After some additional tries it worked. Actually I suppose it was a typo in the OnTriggerEnter-Event (small 'o') and because of unity's dynamic event-dispatch the compiler didn't complain :(.
Nevertheless thanks for your advice :)
Answer by runevision · Feb 23, 2010 at 09:51 AM
As stated in Collider.isTrigger:
A trigger doesn't collide with rigid bodies. Instead it sends OnTriggerEnter, OnTriggerExit and OnTriggerStay message when a rigidbody enters or exits the trigger.
So the Rigidbody needs to be on the object that is not a trigger. From your description, it sounds like you either didn't use Rigidbodies at all, or that you maybe only had the Rigidbody on the collider that has isTrigger set (which would not work).
Does a rigidbody ignore the colliders that are triggers in the same gameobject tree? Because I have noticed that when multiple colliders are attached to the same rigidbody (under the same tree) and all but one are triggers; the normal collider is not registered when entering other trigger colliders outside the tree. (the trigger colliders ignores the normal one with Physics.IgnoreCollision).