- Home /
Detect 2 non kinematic triggers?
Hello! I am making a SHMUP game, and I want the player to lose a life if it collides with an asteroid object. Unfortunately, both the asteroid and the player are triggers (the player doesn't have to be a trigger, but I can't get it to collide when it isn't either), so I can't figure out how to get the 2 triggers to collide. I read that if they have rigidbodies 2 triggers can collide, so I added rigidbodies to both (with gravity disabled), but it's still not working. Here is the code I have attached to my player's body:
#pragma strict
var lives = 3;
function Update ()
{
if(lives < 1)
{
Application.LoadLevel ("GameOver");
}
}
function OnTriggerEnter(other: Collider)
{
if(other.gameObject.tag == 'Enemy')
{
lives -= 1;
}
Destroy(gameObject); //this is only here to test if the collision is working at all and it's not a problem with the above if statement, it's not going to be in the game. It still isn't working
}
Answer by Linus · Jul 04, 2012 at 05:43 AM
Some things to check
First, make sure the enemy is tagged "Enemy". Then make sure once more
When you tested with istrigger turned off, did you change:
function OnTriggerEnter
to
function OnCollisionEnter
The depth, if this is a 2D style game, make sure the object don't pass each other on the depth.
Lastly, if you provide the project (or a video) I am willing to help more. Did a similar project when first starting.
Answer by Piflik · Jun 30, 2012 at 03:17 PM
I never had a problem with two triggers not seeing each other, unless they are both mesh colliders not set to convex. Could that be the case?
Nope, one is a mesh set to convex and the other is a box collider. Want to see my project?
Answer by Berenger · Jun 30, 2012 at 09:24 PM
As you said you added rigidbody for this issue only, I assume you're moving those objects with Translate or position += something. That kind of movement cannot raise collision event, unless it's to move an object with a rigidbody kinematic into an other object with a rigidbody NOT kinematic.
You should use AddForce.
They ignore collisions, but they should still trigger the function. In my game I move everything directly with transform.position and triggers work just fine.
Your answer
Follow this Question
Related Questions
RigidBody - GameObject moves but mesh stays! 1 Answer
The trigger enter detect fast moving object more accurate 2 Answers
Trigger between 2 kinematic bodies not firing? -1 Answers
OnTriggerEnter is not working! I have tried absolutely everything I can think of. 1 Answer
Rigidbody Trigger Collider vs. Static Collider no OnTriggerEnter message 1 Answer