- Home /
Patrol system not working? Ignoring points(?)
So I have a good patrol system, the ony problem is after it hits the second (out of three) points, it goes back to the first and does a whole bunch of weird spins and then goes back to the second point. Not sure why this is happening but here's my code and a gif showing what's happening: https://gyazo.com/8834d49c2c7f31e29d70f866754fcbaf
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PatrolScript : MonoBehaviour
{
public NavMeshAgent cop;
public GameObject Player;
public float FollowDistance = 10.0f;
public Transform[] patrolPoints;
private int currentIndex = 0;
void Start()
{
MoveToNextPatrolPoint();
}
void Update()
{
float dist = Vector3.Distance(Player.transform.position, cop.transform.position);
bool patrol = false;
bool follow = (dist < FollowDistance);
if (follow)
{
cop.SetDestination(Player.transform.position);
}
patrol = !follow;
if ((!follow) && !patrol)
cop.SetDestination(cop.transform.position);
if (patrol)
{
if (!cop.pathPending &&
cop.remainingDistance < 0.5f)
MoveToNextPatrolPoint();
}
}
void MoveToNextPatrolPoint()
{
cop.destination = patrolPoints[currentIndex].position;
currentIndex++;
currentIndex %= patrolPoints.Length;
}
}
Comment