Hello everybody. I attached the following script to an enemy patrolling a maze:
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class Patrol : MonoBehaviour {
public GameObject[] points;
private int destPoint = 0;
private NavMeshAgent agent;
Animator anim;
string state = "patrol";
private Transform play;
private float distance;
private bool chasingPlayer = false;
private float rotSpeed = 1f;
private float rotSpeedPatrol = 0.7f;
private float accuracyWP = 0.1f;
void Start () {
agent = GetComponent();
points = GameObject.FindGameObjectsWithTag ("Path");
anim = GetComponent ();
play = GameObject.FindWithTag("Player").transform;
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint() {
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].transform.position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update () {
// Choose the next destination point when the agent gets
// close to the current one.
Vector3 direction = play.position - this.transform.position;
distance = Vector3.Distance(play.position, transform.position);
direction.y = 0;
if (distance >= 9f) {
state = "patrol";
chasingPlayer = false;
} else if (distance < 9f ) {
//head.transform.LookAt(player.position);
state = "pursuing";
chasingPlayer = true;
} else if (distance < 8f) {
state = "pursuing";
chasingPlayer = true;
}
if(play == null) {
state = "patrol";
chasingPlayer = false;
}
if(chasingPlayer == true) {
state = "pursuing";
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), rotSpeed * Time.deltaTime);
if(direction.magnitude > 5f) {
agent.SetDestination(play.position);
agent.speed = 8f;
anim.SetBool("isAttacking", false);
anim.SetBool("isWalking", true);
anim.SetBool("isIdle", false);
} else {
agent.speed = 0f;
anim.SetBool ("isAttacking", true);
anim.SetBool("isWalking", false);
anim.SetBool("isIdle", false);
}
} else if(chasingPlayer == false) {
agent.speed = 2.25f;
if(state == "patrol" && points.Length > 0) {
anim.SetBool("isIdle",false);
anim.SetBool("isWalking",true);
anim.SetBool("isAttacking",false);
if(Vector3.Distance(points[destPoint].transform.position, transform.position) < accuracyWP)
{
destPoint++;
if(destPoint >= points.Length)
{
destPoint = 0;
}
}
//rotate towards waypoint
direction = points[destPoint].transform.position - transform.position;
if(agent.remainingDistance < 0.2f)
{
WaitAndGo();
}
}
this.transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(direction), rotSpeedPatrol * Time.deltaTime);
}
}
void WaitAndGo() {
GotoNextPoint ();
}
}
I don't understand why, but at a certain point my enemy starts to walk between two waypoints, ignoring the remaining ones. Any suggestions?
↧