Hi ! In situation when moving enemy should ask himself if he is close enough to initiate attack, what is better - having a collider in front of enemy with results OnCollisionEnter or having a raycast when it is alert and searching for player? In case when there are 10-20 units of enemies, What will be more costy ?
For 10-20 units, raycasting or colliders probably doesn’t make all that much different. In which case I would say raycasting, as it wont interfere with other stuff that might be going on.
Distance check + Direction check should be enough. No physx calculation is needed (Including Collision and Raycast)
For distance check, use Vector3.Distance method.
For Direction check, use Vector3.Dot method.
More info : Unity - Scripting API: Vector3.Dot
Actually i know that both components is acceptable and good to implement, But you may consider having a reference to all the enemies and test the distance between the player and them, Using OverlapSphere to determine the accepted distance around the player for inflicting damage, then calculate a direction from you (player) to each enemy inside the sphere and test the angle between the direction vector and player vector, if it is less than 45 for instance then you can inflict the damage.
At least i use this approach and it work fine.
Here is an example:
// all possible enemies in a defined radius
Collider[] hitColliders = Physics.OverlapSphere(player.position, radius);
// check for each enemy
foreach (Collider c in hitColliders)
{
if (c.tag == "Enemy") // it is an enemy within the radius
{
// create a vector pointing from me (player) to the enemy
Vector3 directionToEnemy = c.transform.position - player.position;
// if the angle between player and enemy is less than 45
if (Vector3.Angle(player.forward, directionToEnemy) < 45f / 2f)
{
InflictDamge(); // apply damage
}
}
}
Hope you find it useful