- Home /
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!
Answer by kalvinlyle · Mar 11, 2012 at 05:51 PM
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.
Heh, thanks man ,i looked up this layer collision matrix, and it works perfecly! Thanks again.
Answer by henry96 · Mar 11, 2012 at 02:13 PM
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.
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!
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?
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.
Okay, this is getting ridickolous. I have changed it to "hit.gameObject.layer" , so that it would be easier to deter$$anonymous$$e 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?