ai patrol and chase question
I'm trying to make an enemy ai script where the enemy chooses one of random patrol points to move to and on arrival picks another one. If the raycast hits the player then it chases the player as long as it sees the player. When it stops seeing the player it goes to the players last known location and checks there, then should return to patrolling.
However, on start, the AI goes to the first point, then waits there and doesn't move, even if it sees the player. Clearly there's some sort of loop in my script but for the life of me i can't figure out where. Can someone help me out please?
My script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class aiversion2 : MonoBehaviour
{
bool patrolling;
public Transform[] moveSpots;
private int randomSpot;
public float startWaitTime;
private float waitTime;
public Transform target;
public Transform targetShadow;
RaycastHit sightRayforwards;
RaycastHit sightRaybackwards;
RaycastHit sightRayright;
RaycastHit sightRayleft;
void Patrol()
{
patrolling = false;
startWaitTime = waitTime;
randomSpot = Random.Range(1, moveSpots.Length);
this.gameObject.GetComponent<NavMeshAgent>().SetDestination(moveSpots[randomSpot].position);
new Ray(transform.position, transform.forward * 10);
Debug.DrawRay(transform.position, transform.forward);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
if (sightRayforwards.transform.CompareTag("Player"))
{
Chase();
}
if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.5f)
{
Update();
Patrol();
}
}
void Chase()
{
this.gameObject.GetComponent<NavMeshAgent>().SetDestination(target.position);
while (sightRayforwards.transform.CompareTag("Player"))
{
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
return;
}
Check();
}
void Check()
{
this.gameObject.GetComponent<NavMeshAgent>().SetDestination(targetShadow.position);
while (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance > 0.2f)
{
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out sightRaybackwards);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out sightRayright);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out sightRayleft);
if (sightRayforwards.transform.CompareTag("Player"))
{
Chase();
}
else if(sightRaybackwards.transform.CompareTag("Player"))
{
Chase();
}
else if (sightRayleft.transform.CompareTag("Player"))
{
Chase();
}
else if (sightRayright.transform.CompareTag("Player"))
{
Chase();
}
else
{
return;
}
}
if (waitTime == startWaitTime)
{
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out sightRayforwards);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.back), out sightRaybackwards);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out sightRayright);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), out sightRayleft);
if (sightRayforwards.transform.CompareTag("Player"))
{
Chase();
}
else if (sightRaybackwards.transform.CompareTag("Player"))
{
Chase();
}
else if (sightRayleft.transform.CompareTag("Player"))
{
Chase();
}
else if (sightRayright.transform.CompareTag("Player"))
{
Chase();
}
else
{
waitTime -= Time.deltaTime;
}
if (waitTime <= 0)
{
waitTime = startWaitTime;
}
}
else
{
waitTime = startWaitTime;
}
Patrol();
}
void Start ()
{
waitTime = startWaitTime;
randomSpot = Random.Range(0, moveSpots.Length);
patrolling = true;
}
void Update ()
{
if (patrolling == true)
{
Patrol();
}
}
}
Please use sphere cast ins$$anonymous$$d of raycast. Your code is a disaster.
The loop is in Patrol(). You call Patrol() inside Patrol().
It just loop around this.gameObject.GetComponent<Nav$$anonymous$$eshAgent>().SetDestination(moveSpots[randomSpot].position);
because movespots only have 1 variable.
I'm amaze that Unity not crash