I’ve been trying to detect whether enemies are in line of sight in my game, but for some reason, even though all the raycasting appears to be working properly, it doesn’t seem to want to execute the commands when it verifies that it hit the target.
var hit : RaycastHit;
var enemies : GameObject[];
enemies = GameObject.FindGameObjectsWithTag("Enemy");
for (var enemy : GameObject in enemies)
{
var dummyVec : Vector3;
dummyVec = enemy.transform.position - transform.position;
Debug.DrawRay(transform.position, dummyVec, Color.red);
if (Physics.Raycast (transform.position,dummyVec, hit)){
print(hit.transform.tag);
if (hit.transform.tag == Enemy)
{
enemyLoS = true;
print("ouch");
}
}
}
I’ve also tried setting the if statement to “if (hit.transform = enemy)” as well as that is the name of the game object variable it is testing in that iteration of the for loop.
If anyone could shed any light on my conundrum I would really appreciate it.
Thank you.