So I want to stop my enemy from sticking its head through the wall when it chases the player into the save room. I have a collider on the enemy and the wall, but that doesn’t stop it. I also can’t move back the navmesh because then the enemy will stand in front of the player before they enter the save room and it won’t look right. How can I get this to work right?
public class MutantRatBrain : MonoBehaviour
{
[SerializeField] float wanderRange;
[SerializeField] float alertRange;
[Space]
[SerializeField] Transform[] waypoints;
int currentWaypointIndex = 0;
[SerializeField] Transform playerGuy;
[HideInInspector] public string state = "Wander";
float range;
float waitTime = 6f;
float waitTimer;
float waitTime_2 = 6f;
float waitTimer_2;
[HideInInspector] public NavMeshAgent nav;
Animator anim;
public bool canChase;
bool wait;
bool wait_2;
bool canWander = true;
void Start()
{
nav = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
waitTimer = waitTime;
waitTimer_2 = waitTime_2;
wait = true;
wait_2 = false;
canChase = false;
range = wanderRange;
}
void Update()
{
if (state == "Idle")
{
waitTimer -= Time.deltaTime;
if (waitTimer <= 0)
{
canWander = true;
anim.SetBool("Idle", false);
state = "Wander";
waitTimer = 0;
}
}
if (state == "Wander")
{
if (canWander)
{
range = Vector3.Distance(transform.position, waypoints[currentWaypointIndex].position);
Wander();
}
if (nav.remainingDistance <= nav.stoppingDistance && !nav.pathPending)
{
currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
anim.SetBool("Idle", true);
state = "Idle";
}
}
if (state == "Alert")
{
range = alertRange;
Search();
}
if (canChase)
{
state = "Chase";
}
if (state == "Chase")
{
if (nav.pathStatus == NavMeshPathStatus.PathInvalid || nav.pathStatus == NavMeshPathStatus.PathPartial)
{
canChase = false;
anim.SetBool("Idle", true);
anim.SetBool("Chase", false);
nav.ResetPath();
state = "Idle";
}
else
{
nav.destination = playerGuy.position;
nav.speed = 6f;
anim.SetBool("Chase", true);
FaceTarget();
}
}
}
void Wander()
{
canWander = false;
waitTimer = waitTime;
nav.speed = 3f;
nav.SetDestination(waypoints[currentWaypointIndex].position);
}
void Search()
{
canWander = false;
nav.speed = 3f;
Vector3 randomPos = Random.insideUnitSphere * range;
NavMeshHit navHit;
int acceptableArea = 1 << NavMesh.GetAreaFromName("IsWalkable");
int forbiddenArea = 1 << NavMesh.GetAreaFromName("Corner");
int walkableArea = acceptableArea;// & ~forbiddenArea;
if (NavMesh.SamplePosition(transform.position + randomPos, out navHit, wanderRange, walkableArea))
{
nav.SetDestination(navHit.position);
Vector3 destination = navHit.position;
Vector3 direction = destination - transform.position;
RaycastHit hit;
if (Physics.Raycast(transform.position + Vector3.up * 1.5f, direction, out hit, direction.magnitude))
{
// If the raycast hits an obstacle, adjust the destination
destination = hit.point - direction.normalized * nav.stoppingDistance;
}
}
}
IEnumerator ReturnToWaypoints()
{
yield return new WaitForSeconds(4f);
canWander = true;
state = "Wander";
}
public void FaceTarget()
{
Vector3 direction = (playerGuy.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0.0001f, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(transform.position, range);
}
}
public class FieldOfView : MonoBehaviour
{
public float viewRadius;
[Range(0, 360)]
public float viewAnlge;
public LayerMask targetMask;
public LayerMask obstacleMask;
MutantRatBrain ratCreature;
[HideInInspector]
public List<Transform> visisbleTargets = new List<Transform>();
void Start()
{
ratCreature = GetComponent<MutantRatBrain>();
StartCoroutine("FindTargetsWithDelay", 0.2f);
}
IEnumerator FindTargetsWithDelay(float delay)
{
while (true)
{
yield return new WaitForSeconds(delay);
FindVisibleTargets();
}
}
void FindVisibleTargets()
{
visisbleTargets.Clear();
Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);
for (int i = 0; i < targetsInViewRadius.Length; i++)
{
Transform target = targetsInViewRadius[i].transform;
Vector3 dirToTarget = (target.position - transform.position).normalized;
if (Vector3.Angle(transform.forward, dirToTarget) < viewAnlge / 2)
{
float dstToTarget = Vector3.Distance(transform.position, target.position);
if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask))
{
visisbleTargets.Add(target);
if (ratCreature.nav.pathStatus == NavMeshPathStatus.PathInvalid || ratCreature.nav.pathStatus == NavMeshPathStatus.PathPartial)
{
Debug.Log("No Path");
ratCreature.canChase = false;
}
else
{
Debug.Log("Path");
ratCreature.canChase = true;
}
}
}
}
}
public Vector3 DirFromeAnlge(float angleInDegrees, bool angleIsGlobal)
{
if (!angleIsGlobal)
{
angleInDegrees += transform.eulerAngles.y;
}
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
}