- Home /
Projectile/Shield Collisions with Friendly Projectiles Passing Through
I've been having trouble getting colliders and triggers to work with shields. This is for an RTS-style game, and I want projectiles fired from player weapons to pass through shields as if the shield wasn't there, but projectiles fired from enemy weapons to impact the shield.
My projectiles have a simple box collider and a rigidbody (non-kinematic), and my shield has a mesh collider set as a trigger with no rigidbody.
From the table at the bottom of the collider pages in the manual (i.e., http://unity3d.com/support/documentation/Components/class-BoxCollider.html ), it looks like I should be receiving OnTriggerEnter events on the shield when a projectile collides with the shield, but instead, I'm getting an OnCollisionEnter event on the projectile.
I've also tried using a simple sphere collider for the shield, but I get the same result.
How should the colliders be set up so the trigger is called instead of the collider?
Answer by Berenger · Dec 21, 2010 at 09:51 AM
Maybe you could work this out with link text ?
Or another way, but it sounds weird : When you get an OnCollisionEnter and if this is an ally, set the isTrigger parameter to true. I'm not sure of that approch, but it can't hurt talking about it :)
This may have changed in Unity 3 (I'm not sure), but I know in earlier versions by the time OnCollisionEnter is called the collision has already happened, which made doing things like IgnoreCollision useless (which sounds similar to what you've suggested). I tried this once before, and if an artillery shell hit the shield, it would just stop (because it collided with the shield) and drop to the ground, even though the OnCollisionEnter code told it to ignore collisions with the shield.
The layer-based collisions are too general because I need to handle this on a per-projectile basis. I don't want a friendly projectile to hit the shield, disable shield/projectile collisions, then have an enemy projectile pass through before the friendly shell has exited. I'd also have to somehow keep up with when a friendly projectile enters and exits the shield, which would be difficult when collisions between the two are disabled.
However... I could probably set up layers for each player (i.e., Player1Projectile, Player2Projectile, ..., Player1Shield, Player2Shield, ...) and set up collisions properly between them.
I think I'm going to try that out and get back to you.
Well, it used a lot more layers than I liked (2 * the number of players), but it worked perfectly, so you win.
If anyone else has another option (I'd really like one that uses less layers), I'd still appreciate it.
Your answer
Follow this Question
Related Questions
Do you have to have a rigidbody component for collision detection? 1 Answer
No Collision 3 Answers
AI Players Movement for Rolling Ball Game 0 Answers
Trouble with Physics.IgnoreCollision 0 Answers
Big issue with colliders 1 Answer