I’m using Physics.Linecast to check a line between my enemy and the player (going from the enemy to the player), and it seems that if the line reaches the player without being intersected on the way then it passes and the code that relies on it passing executes, but if the line hits anything on the way to the player (like a wall) then it fails and the code that relies on it passing doesn’t execute. This is actually what I want it to do, and this is the code I’m using:
if (Physics.Linecast(transform.position,
player.transform.position))
{
canHitPlayer = true;
Debug.Log("Can hit player");
}
else
{
canHitPlayer = false;
Debug.Log("Cannot hit player");
}
But this seems to be the exact opposite of what the unity documentation says Physics.Linecast does, where the code should return true if the line hits anything in between the start and end points, so it should be false if it reaches the end point unimpeded.
Any ideas what’s up with that?
And the reason I ask is because my code above does seem to work the way I want it to in most cases (even though it seems to be the opposite of what the documentation says): If the line is uninterrupted going from the enemy to the player, so it doesn’t hit any scenery or whatever, then the enemy will fire at the player–that’s what I want. And if the line is interrupted then they won’t fire at the player–that’s also what I want. Except the enemies seem to be able to shoot right through each other for some reason, when I would expect the line check to be stopped by other enemies and therefore prevent them from firing at the player unless they have a clear line of sight–that is not what I want.
Note: The enemies all have collision boxes on their parent objects, so I can’t figure out why the code above seems to be working for the most part but not when enemies try to shoot and there is another enemy in the way, which should stop them from actually firing but doesn’t.
Am I just doing this all wrong, and if so, what’s the correct way to go about this?