Reputation: 31
I'm currently making a first person shooter with me, the player, fighting against zombies. So I have ammo boxes scattered around the map with this function:
void OnTriggerEnter(Collider other)
{
AmmoSound.Play();
if (Ammo_count.LoadedAmmo == 0)
{
Ammo_count.LoadedAmmo += 10;
this.gameObject.SetActive(false);
}
else
{
Ammo_count.CurrentAmmo += 10;
this.gameObject.SetActive(false);
}
}
This code works great as I have a mesh collider on my character that is "Is trigger" and "convex"; but the problem I'm having is that my zombie is also able to pick up the ammo. This creates several other problems in my scripts. So is there any way to stop the zombies from being able to pick up the ammo? The zombie mesh collider is not "Is trigger"; but it can still pick up the ammo.
Upvotes: 3
Views: 6162
Reputation: 7215
The easiest way to solve your problem would be to utilize tags as mentioned in the comments. Add a "player" tag to your player gameobject and wrap your ammo pickup code in an if statement as shown below:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "player")
{
AmmoSound.Play();
if (Ammo_count.LoadedAmmo == 0)
{
Ammo_count.LoadedAmmo += 10;
}
else
{
Ammo_count.CurrentAmmo += 10;
}
this.gameObject.SetActive(false);
}
}
To answer the question as written: you can have certain collisions ignored by modifying the layer collision matrix. For example, you could apply a layer to your ammo boxes called "pickup" and a layer to your zombies called "enemy". You could then modify your layer collision matrix so that the enemy and pickup layers do not interact.
Upvotes: 6
Reputation: 21
Use Tags for gameobjects which need to ignored then on the on ontrigeerenter method just check for the tag ignore it
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag != "<-tag need to be igonred->") {
// Your work for here
}
}
Upvotes: 0