AI Raycasting Patrol and Chase,AI enemy raycast help
I have an AI that patrols over the navmesh to certain objects at random around the scene, it goes to one then waits 'x' seconds before picking another and moving on (this works fine). I have been trying to get it to raycast in front then change the destination to the player as soon as it hits something tagged 'Player' (again, can do this on its own), then check the last seen location of the player when it loses sight.
My current workaround for this is to have a cube that matches my players coordinates whenever the player gets hit by raycast, and although this sort of works (it follows the cube until it loses sight of it) then immediately carries on patrolling instead of waiting 'x' seconds.
I'm quite new to this but would appreciate any help.
This is my script though it is very jumbled since I've been trying to find solutions:
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;
}
}
}
}
,I am trying to make an enemy AI that patrols (via NavMesh) to certain points around the area, waiting 'x' seconds before moving on to the next location (this bit works fine on its own) then has a raycast that changes the destination of the navmesh agent when hitting objects tagged with 'Player'. Then, when the raycast no longer hits the player, the destination is changed to the last seen position of the player.
My current workaround was to create a box that matched the players position every time the player made contact with the raycast, which works, if the enemy hits the box on raycast it follows it, but no matter the distance away as soon as it loses the raycast it goes back to patrolling.
I am quite new here but would like some help. My code is quite messy atm because I've played around with it so much looking for a solution but it might be of some help:
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
Follow this Question
Related Questions
AI chase/follow problem 0 Answers
How do I affect multiple objects in a single click? 0 Answers
NavmeshDestination not changing. 0 Answers
Problem with NavMesh 0 Answers
How change the destination point if it is inside an obstacle? 0 Answers