I made an AI that walks around a maze and when it sees the player it starts chasing him but the only problem is that the AI can see the player through the walls
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class MazeAI : MonoBehaviour
{
GameObject player;
public NavMeshAgent agent;
public float range;
public Transform centrePoint;
[SerializeField] float sightRange, attackRange;
bool playerInSight, playerInAttackRange;
[SerializeField] LayerMask groundLayer, playerLayer;
void Start()
{
agent = GetComponent<NavMeshAgent>();
player = GameObject.Find("Player");
}
void Update()
{
if (agent.remainingDistance <= agent.stoppingDistance)
{
Vector3 point;
if (RandomPoint(centrePoint.position, range, out point))
{
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f);
agent.SetDestination(point);
}
}
playerInSight = Physics.CheckSphere(transform.position, sightRange, playerLayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, playerLayer);
if (playerInSight && !playerInAttackRange) Chase();
}
bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range;
NavMeshHit hit;
if (NavMesh.SamplePosition(randomPoint, out hit, 1.0f, NavMesh.AllAreas))
{
result = hit.position;
return true;
}
result = Vector3.zero;
return false;
}
void Chase()
{
agent.SetDestination(player.transform.position);
}
}