- Home /
Simple Colliders Question
I'm new to colliders and I'm having trouble getting them to work.
I have an empty game object with a box-collider component. It is parented to a player controller and therefore follows them around.
There are other empty game objects through the scene with box-collider components attached. They are static (not moving) and so a rigid body cannot be added to them.
I want to detect when the player-attached collider and the world colliders collide.
Note: Because I thought my terrain colliders (among others) might get in the way, I thought of suspending these other colliders 100m in the air along with the parented one.
I have tried using onCollisionEnter, but can't seem to get it to work. I've tried different combinations of 'is trigger' as well. Please help!
Which one is the trigger? Which one should have the script attached? What should the script look like? Do either of them NEED to be tagged? :(
EDIT: ALSO! Please note I've considered using Vector distance, and when the distance between the two objects is less than a certain distance I could trigger the event that way, but there will be thousands of these floating game objects, and vector distance would probably use more processing power than onCollisionEnter + Exit... or would it?
Answer by Berenger · Jan 30, 2013 at 09:50 AM
A collision event between two colliders is raised only if the movement was made by the physic engine, like the gravity on a rigidbody. In your case, parenting is moving that empty game object. You need to add the collider on your player.
Or, there is the kinematic notion. Set a rigidbody on that empty game object, child of the player, with a box collider. The rigidboy must be kinematic, without gravity, and frozen on everything. Set the collider as trigger, and you'll get the OnTriggerXXX events.
Thanks! It was detected! Sadly, however, when I tried switching the roles so that the one with the Rigid body was stationary and the one without was moving, it didn't work. That's okay though, I think I'll be able to make this work. :)
Quick follow up question: Now that the two have collided, how do I get the transform position of the object I collided with? (note: not the actual collision location)
Never$$anonymous$$d! Solved it with this:
r = other.transform.position.x;
c = other.transform.position.z;
Instantiate (fLow1, Vector3(r, 0, c), transform.rotation);
Next, however, I need onTriggerExit to destroy just that object that was instantiated. I tried:
function OnTriggerExit ( other : Collider )
{
Destroy ( other );
}
But, alas, no luck.
Just realised the above code is destroying the box collider and not the Instantiated object. Gah! Assigning a name to the instantiated object is infeasible because there will thousands of them, many of them clones.
Follow-up question continued at: http://answers.unity3d.com/questions/390503/remove-object-based-on-its-position-alone.html
Destroy ( other.gameObject );, if you want to destroy the object entirely.
Your answer
Follow this Question
Related Questions
Extracting child trigger from compound collision 0 Answers
Box Trigger and Mesh Collider on one object. 1 Answer
Using shadows as Triggers? 1 Answer
How to restart scene with trigger? 1 Answer
Collider won't collide with a trigger 3 Answers