- Home /
OnTriggerEnter one collider only
quite simply when there's 2 object top of another when u shoot a bullet, it will hit both object colliders and i want the bullet to only hit one object; i've been looking around couldnt find a simple fix plz. any tip would help as well
(example: i shoot a bullet at 2 enemies being on top of another), that bullet will hit both enemies, but i want to bullet to collide with only one enemy object.
i want the bullet i shoot to hit only one enemy and not both
function OnTriggerEnter (Missile : Collider)
{
if(Missile.tag == "Enemy")
{
//error one missile hitting more than one collider
Player.missileCount = Player.missileCount + 1;
Destroy(gameObject);
}
}
as u can see, if the bullet hits 2 enemies; the value for Player.missileCount will increase by 2 instead of 1 which is a no no for me
why not? im saying when two object one top of another gets hit by an object(example: i shoot a bullet at 2 enemy being on top of another), that bullet will hit both enemies right?
i want the bullet i shoot to hit only one enemy and not both
Answer by whydoidoit · May 31, 2012 at 01:04 AM
Hang on - you just want to only count the hit once?
var hitEnemy = false;
function OnTriggerEnter (Missile : Collider)
{
if(Missile.tag == "Enemy" && !hitEnemy)
{
hitEnemy = true;
//error one missile hitting more than one collider
Player.missileCount = Player.missileCount + 1;
Destroy(gameObject);
}
}
:///// smashing my head against wall..... my moto when i program usually is: simple is better but this is ridiculous xD or maybe cause its 3am.. anyway thx a bunch dude, now i can go to sleep feeling like a happy retard. thx for reptilebeats for all the help as well anyway
Answer by reptilebeats · May 30, 2012 at 10:20 PM
im going to take a wild stab in the dark as i dont really understand the question, but as far as i can work out is you only want a bullet to hit one collider and pass through or ignore the other,
this can be achieved very easyily through the use of layers, simpley make a new layer and call it something like bullet and then make another one for objcets you either want it to hit or not ht.
then go into physics settings and uncheck the relevant boxes google layers to get an understanding of how they work to achieve this.
http://unity3d.com/support/documentation/Components/Layers
in short its for what coliddes with what
thx but not exactly wat im looking for, maybe read my 2nd comment probably is simpler to understand; in this case i do want to hit every object in the scene, just not at the same time.
wat about this, it will only do something to a certain object with a name
function OnCollisionEnter(col:Collision){
if(col.gameObject == gameObject.Find("the object")) ///u could use tags ins$$anonymous$$d for multiple objects
do something
}
well dont think it would work since there are enemy prefab clones, updating question for other people.
you can rename the prefabs to any name that you would like so that the reference still works