x


Hit Collision with exceptions

Hello

I am trying to make a projectile, which destroys itself upon impact. The problem is ,i need it to ignore some objects ,such as the character controllers collider, and i am flummoxed why it wont work. My solution is:

function OnCollisionEnter ( hit : Collision){

if (hit.gameObject.tag !== "MainCamera" || hit.gameObject.tag !== "DontDestroyProjectiles" ) {

Destroy (gameObject);

}

}

Also i have been fiddling with Physics.IgnoreCollision ,without results. Maybe it would work with that, and if someone found a way with this ,please explain it troughly, becouse im pretty dense.

Any help would be greatly appriciated. Thanks!

more ▼

asked Mar 11 '12 at 01:00 PM

Sir Mate gravatar image

Sir Mate
64 6 9 12

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

2 answers: sort voted first

Use physics layers if you want to ignore collisions between types of objects. It doesn't require coding and it will be faster as the collision will not take place and therefore no code will be run.

more ▼

answered Mar 11 '12 at 05:51 PM

kalvinlyle gravatar image

kalvinlyle
241 10 16 19

Heh, thanks man ,i looked up this layer collision matrix, and it works perfecly! Thanks again.

Mar 11 '12 at 06:49 PM Sir Mate
(comments are locked)
10|3000 characters needed characters left

I find a bit of issue in your coding. I don't know if that's the cause of the problem. You should use "!=" instead of "!==" . Also, since this script is for projectile, I suggest you use Trigger because Collision sometimes causes slowness to the object.

more ▼

answered Mar 11 '12 at 02:13 PM

henry96 gravatar image

henry96
597 16 20 23

I changed it to != , didnt change a thing. Also, i need colliders ,becouse i want the projectile to influence rigidbodies;which it does, the box goes flying, only it destroys itself upon connecting with and colliders. Thanks for the reply!

Mar 11 '12 at 02:24 PM Sir Mate

If the change of !== to != did not change anything, it means your problem is somewhere else becuase it should have made a difference. Try to start with only the collison function and destroy and see how it interacts. Then start adding condition until it goe sthe right way. Also, I might be stupid but I don't really get what is happening. Is the projectile exploding or not and whether or not it explodes what is happening to it and around?

Mar 11 '12 at 03:39 PM fafase

That's strange because I think the code is fine the way it is. Go over what you have done, maybe it gives you some clues.

I also find a bit of issue with Unity 3.5 scripting. Some script works fine in 3.4.2 but kinda not working in 3.5.

Mar 11 '12 at 03:41 PM henry96

!== is not fine. != is fine or == is fine.

Mar 11 '12 at 03:59 PM fafase

Okay, this is getting ridickolous. I have changed it to "hit.gameObject.layer" , so that it would be easier to determine the problem, so my code is now:

function OnCollisionEnter ( hit : Collision){

if (hit.gameObject.layer == "Something" ) {

Destroy (gameObject);

}

}

But its working completely ass-backwards! In this case ,nothing destroyes the projectiles, not even the layered objects, whereas if i change it to != than EVERYTHING destroyes them ,again ,even the layered objects.

What kind of sorcery is this?

Mar 11 '12 at 04:44 PM Sir Mate
(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:

x5049
x2482
x1936
x1682
x762

asked: Mar 11 '12 at 01:00 PM

Seen: 718 times

Last Updated: Mar 11 '12 at 06:49 PM