I’ve created a script now that can detect all enemies in a radius. In order to stop enemies who are behind walls from being harmed by the explosion radius, a raycast is sent out in order to ensure that it hits an enemy and not a wall. Unfortunately, if there are two enemies in the same direction, then two raycasts are going to be sent towards the same enemy, thus hitting them multiple times.
_
My question is: How can I stop an enemy standing in front of another enemy from being hit by a raycast that is intended to hit the object behind it?
if(collision.gameObject.tag == "Enemy") { collision.gameObject.SendMessage("takeDamage", 125); }
grenadeBody.velocity = Vector3.zero;
Collider[] collisionsInRadius = Physics.OverlapSphere(transform.position, 3f); //Needs layermask so it only detects gameobjects with enemy tag.
for(int i = 0; i < collisionsInRadius.Length; i++)
{
Ray explosionRay = new Ray(transform.position, collisionsInRadius*.transform.position - transform.position);*
if (Physics.Raycast(explosionRay, out RaycastHit explosionHit, 3f))
{
if(explosionHit.collider.gameObject.tag == “Enemy”)
{
collisionsInRadius*.gameObject.SendMessage(“takeDamage”, 225);*
}
}
}
//play explosion effect.
Destroy(this.gameObject);