Line Of Sight - should work, but doesn't

I’m working on simpliest line of sight mechanic for enemies. It consists of few elements:

  • In front of the enemy there is a Trigger Sphere. If player enters it, the trigger sends message to Enemy, that player is in range of sight.
  • Then, Enemy has a simple Raycast script to check, if there is something between him and Player. If there isn’t, he starts chasing the player.

Simple as that. Everything works great through the Sphere Trigger, but Raycast doesn’t. It just does nothing. No detection of player at all.

The Script for raycast detection:

function CheckLineOfSight(){

	var hit : RaycastHit;
	var rayDirection = Player.transform.position - transform.position;
	  Debug.Log("2");
		 if (Physics.Raycast (transform.position, rayDirection, hit)) {
				if (hit.transform.tag == "Player") {
 		Debug.Log("3");
					GETHIM();
				}
		}
		
}

Debug Log “2” works and shows up when Player enters Trigger Sphere. Debug Log “3” doesn’t show at all. No errors too.

Researching google gave me no working answers.

Am I missing something?

AAAAH, works now!
“Sometimes issues occur if your ray cast starts inside a collider.”
THAT was the issue. The Rayast was hitting the Trigger Sphere. I just set the trigger to layer “Ignore Raycast” and now it works perfectly. Thanks a lot to everyone!
The Linecast will be most useful in the future too. :slight_smile: