Issue with enemies shooting each other...

Hello.

The enemies in my game are trying to shoot the Player regardless of whether or not another enemy is in the way. hence, enemy “friendly fire” occurs which is sometimes funny to see and to some extent realistic but I want to avoid it. :?

Hence, i put in a Physics.Linecast statement to check if there is something blocking the player before the enemy fires.

if (!Physics.Linecast (transform.position, objPlayer.transform.position))
{
shootPlayer();
}

Unfortunately, this isn’t working for me because apparently there is something ALWAYS in the way and so the enemy no longer fires at anything. I suspect it could be the actual rigid body of the enemy object but I am not sure how to check for this.

Should I be using layer masks to solve this issue?

If anyone can offer some insight or point me in the right direction, I would really appreciate it. :smile:

thank you in advance,

Yep.

There’s even a nice example of it in the Layers documentation.

Thank you, Quietus.

Do you happen to know of any other methods to address this issue? I was hoping to do it with code, but if this is pretty much the best way to do it, I will try it. :smile:

Thank you, Quietus.

Do you happen to know of any other methods to address this issue? I was hoping to do it with code, but if this is pretty much the best way to do it, I will try it. :smile:

Well it is kind of doing it in code, as you’re referencing the bitmask in the function call. It’s just that you use a GUI to set up the bitmask instead of a bunch of (1 << n) macros like in Ansi-C.

This sort of issue crops up a lot when people are firing projectiles with a rigidbody from a gun. In those situations you do need to use code, and call IgnoreCollision on each offending object.