x


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?

more ▼

asked Dec 21 '10 at 05:27 AM

Weev gravatar image

Weev
65 4 5 11

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 :)

more ▼

answered Dec 21 '10 at 09:51 AM

Berenger gravatar image

Berenger
11k 12 19 53

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.

Dec 21 '10 at 04:48 PM Weev

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.

Dec 21 '10 at 04:51 PM Weev

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.

Dec 21 '10 at 04:52 PM Weev

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.

Dec 21 '10 at 05:03 PM Weev
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2506
x1881
x1707
x21

asked: Dec 21 '10 at 05:27 AM

Seen: 1304 times

Last Updated: Dec 21 '10 at 05:27 AM