- Home /
how to collide while having a trigger?
I have a script that uses OnTriggerEnter to detect a collision. The problem is the thing it collides with goes right through it. Is there a way to use OnTriggerEnter and not have the thing it collided with go through it?
Answer by ThePunisher · Feb 11, 2013 at 09:02 PM
The trigger functionality is simply for the purpose of being notified when a rigidbody has entered a particular area covered by a collider, meaning there won't be collisions with the collider.
What you want to do is disable the isTrigger checkbox on the collider and define the OnCollisionEnter method in your script. Like this:
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html?from=Collision
Then, you will have your collision handled while being able to script something once the rigidbody has entered the collider.
You need to make sure that your Player/Object has a rigidbody and the object it is colliding has a collider component. Also, make sure the script with the OnCollisionEnter is on the object with the collider.
Did as you said; is Trigger box is unchecked, the player has a rigidbody, the object it collides with has a box collider, and the code looks like this:
void OnCollisionEnter(Collision collision) {
Debug.Log("Collision Detected");
if(rigidbody.gameObject.tag == "Weapon") {
Debug.Log("Damage Delt");
EnemyLife -= Damage;
}
}
When I add the rigidbody to the player it starts acting strange. The y axis will just keep getting larger and when I lock the y axis it moves erratically.
Oh woops, forgot to tell you to uncheck the uses Gravity checkbox on the rigidbody.
The player still floats. :( On another note does everything else look good?
I put the rigidbody on a child and shut off gravity. The player moves normally now, but the debug and if statement don't run.
Hmm, did you make sure isTrigger is disabled on the collider that has your debug statement script?
Answer by adrenak · Feb 10, 2013 at 05:55 AM
Use OnCollisionEnter in the script and disable the 'isTrigger' checkbox so that it becomes a normal collider. When the 'thing' collides, the script will do its job.
PS : Just keep in mind that the OnTriggerEnter function returns a Collider variable, where as the OnCollisionEnter returns Collision variable, just something that people forget sometimes. Look it up in the scripting manual.
Your answer
Follow this Question
Related Questions
Increase Speed 1 Answer
Trigger and Collider Issues 1 Answer
OnTriggerEnter gives me error? 2 Answers
Disable Mesh Collider Trigger 1 Answer
OnTriggerEnter fails to activate 1 Answer