AI Raycast sight chase + last seen location
I am trying to make an enemy AI that patrols through multiple different waypoints, waiting 'x' seconds at each one then choosing another at random (this bit works fine). When the raycast firing from the front of the enemy hits the player then it sets the destination to the player, and when it loses sight of the player it goes to the last known location of the player.
Currently my solution for this has been creating a cube that matches the players location while he is in the raycast, then stays put when the player isn't (therefore the enemy targets the cube not the player). However, although the enemy does change its destination to the player(cube) when it enters the raycast, it continues to go to the patrols points as soon as the player leaves line of sight.
Can someone help me so that it goes all the way to the player(cube) and waits for two seconds to see where they are?
My current code is here (although it's jumbled as i was trying to find a solution):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class basicAI : MonoBehaviour
{
public Transform[] moveSpots;
private int randomSpot;
private float waitTime;
public float startWaitTime;
public float raydist;
Ray sightRay;
RaycastHit rayHit;
public Transform enemy;
public Transform lastSighting;
// Use this for initialization
void Start()
{
waitTime = startWaitTime;
randomSpot = Random.Range(0, moveSpots.Length);
lastSighting.position = enemy.transform.position;
}
// Update is called once per frame
void Update()
{
this.gameObject.GetComponent<NavMeshAgent>().destination = moveSpots[randomSpot].position;
sightRay = new Ray(transform.position, transform.forward * raydist);
Debug.DrawRay(transform.position, transform.forward * raydist);
Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out rayHit);
if (rayHit.transform.CompareTag("Player"))
{
lastSighting.transform.position = rayHit.point;
this.gameObject.GetComponent<NavMeshAgent>().destination = lastSighting.transform.position;
if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.2f)
{
if (waitTime <= 0)
{
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
if (this.gameObject.GetComponent<NavMeshAgent>().remainingDistance < 0.2f)
{
if (waitTime <= 0)
{
randomSpot = Random.Range(0, moveSpots.Length);
this.gameObject.GetComponent<NavMeshAgent>().destination = moveSpots[randomSpot].transform.position;
waitTime = startWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612172734im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NavMesh OffMeshLink only use if a condition is meet 0 Answers
how to search for a random point until the condition is true? 0 Answers
I need the Nav Mesh Stop , I need the Animation to Play Javascript 0 Answers
NavMeshAgent sliding bug 1 Answer
How do I get my guard(s) to stop at NavPoint(s) and go into IdleState in C# 0 Answers