Hello. I am trying to make a raycast with my enemy to where once the player goes in his line of sight he begins to shoot. I have achieved this with a box collider trigger, but can’t figure out the raycast! I inserted a picture of the code I have so far. It is not working in that “Player in Sight” only happens once the player is actually touching the enemy (I have colliders on both the enemy and player, could that be affecting it?). Also, the enemy is still noticing the player even if the player is behind a wall. I am open to switching around layer masks but even when I tried to set one up (not pictured below) it still didn’t work. Please help!
private void Update()
{
SpotPlayer(playerPos);
}
private void SpotPlayer (Transform playerPos)
{
if (Vector3.Distance(transform.position, playerPos.position) < viewDistance)
{
Vector3 directionToPlayer = (playerPos.position - transform.position - transform.position).normalized;
float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, directionToPlayer);
if (angleBetweenGuardAndPlayer < fieldOfViewAngle / 2)
{
Debug.Log("viewAngle" + fieldOfViewAngle);
foreach(RaycastHit hit in Physics.RaycastAll(transform.position, playerPos.position))
{
Debug.Log(hit.collider.gameObject);
}
if (!Physics.Linecast(transform.position, playerPos.position)
{
playerInSight = true;
nav.enabled = false;
Debug.Log("Player in Sight");
}
}
}
playerInSight = false;
nav.enabled = true;
Debug.Log("Player not in sight");
}