Take the above image.
I want to raycast to check if zombie 1 is in sight of the wizard (ignoring if other enemies are in the way). If i raycast with a layermask that contains the block layer and the enemy layer, the raycast will hit zombie 2 and therefore believe zombie 1 is in sight, when it is not.
How do i raycast and only hit one object from the layer? Or solve my problem another way?
Cheers.
Raycasting code below:
public bool AttemptShield(){
GameObject player = GameObject.FindGameObjectWithTag("Player");
Transform shieldTarget = null;
Collider2D[] enemiesInRange = Physics2D.OverlapCircleAll(transform.position, shieldRange, LayerMaskStorage.instance.enemyMask);
if(enemiesInRange == null){
return false;
}
// Sort by dist to player
var enemiesInRangeSorted = enemiesInRange.OrderBy(x => Vector2.Distance(player.transform.position, x.transform.root.position)).ToList();
foreach (var enemy in enemiesInRange)
{
// Check if enemy has shield already
bool hasShield = false;
if(enemy.transform.root.Find("Shield"))
hasShield = true;
// Check if enemy is in view
bool inSight = false;
var dir = (enemy.transform.position - transform.position).normalized;
var hit = Physics2D.Raycast(transform.position, dir, shieldRange, LayerMaskStorage.instance.enemyBlockMask);
if(hit)
if(hit.transform.tag == "Enemy")
inSight = true;
//If enemy is in sight and doesn't have a shield, shield it and return
if(inSight && !hasShield){
Shield(enemy.gameObject);
return true;
}
}
return false;
}