Simple AI for Unity to patroll and follow the player
Well hello there I´m making a small game where there is a demon that has to follow a patrol area and when it sees the player it has to chase it. After watching some tutorials I have created the code for the demon to patrol and to follow the player. But I haven´t been able to make the demon do the 2 things. So my code looks like this.
public class Gozu : MonoBehaviour
{
//Dictates whether the agent waits on each node
[SerializeField] private bool patrolWaiting;
//The total time we wait at each node
[SerializeField] private float totalWaitTime = 3f;
//The progagility of switching direction.
[SerializeField] private float switchProbability = 0.2f;
//The list of all patrol nodes to visit
[SerializeField] private List<Waypoint> patrolPoints;
//private variables for base behaviour
private NavMeshAgent navMesh;
private int currentPatrolIndex;
private bool travelling;
private bool waiting;
private bool patrolFoward;
private float waitTimer;
public Transform player;
private Animator anim;
private string state = "IDLE";
private Vector3 direction;
private float angle;
private float rotationSpeed = 2.0f;
private float speed = 2.0f;
private float visDist = 20.0f;
private float visAngle = 30.0f;
private float attackDist = 1.0f;
// Start is called before the first frame update
void Start()
{
//get the animations to trigger them
anim = this.GetComponent<Animator>();
//gets the NavMesh for the patrol
navMesh = this.GetComponent<NavMeshAgent>();
//starts the count of the patrol points and makes the demon start on a point
if (patrolPoints != null && patrolPoints.Count >=2)
{
currentPatrolIndex = 0;
SetDestination();
}
}
// Update is called once per frame
void Update()
{
//gets the updated position of the player
direction = player.position - this.transform.position;
//get the angle foward of demon
angle = Vector3.Angle(direction, this.transform.forward);
//evaluates if the player is in the cone of vision of the demon
if (direction.magnitude < visDist && angle < visAngle)
{
FollowPlayerCone(direction);
}
else
{
PatrolGozu();
}
//SeekPlayer(player.transform.position);
}
//sets the destinatios of patrol points
private void SetDestination()
{
if (patrolPoints != null)
{
Vector3 targetVector = patrolPoints[currentPatrolIndex].transform.position;
navMesh.SetDestination(targetVector);
travelling = true;
}
}
//changes the patrol point
private void ChangePatrolPoint()
{
if (UnityEngine.Random.Range(0f, 1f) <= switchProbability)
{
patrolFoward = !patrolFoward;
}
if (patrolFoward)
{
currentPatrolIndex = (currentPatrolIndex + 1) % patrolPoints.Count;
}
else
{
if (--currentPatrolIndex < 0)
{
currentPatrolIndex = patrolPoints.Count - 1;
}
}
}
//follows the player f is in cone/angle of vision
private void FollowPlayerCone(Vector3 direction)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
if (direction.magnitude > attackDist)
{
if (state != "RUNNING")
{
state = "RUNNING";
anim.SetTrigger("isRunning");
}
}
else
{
if (state != "ATTACKING")
{
state = "ATTACKING";
anim.SetTrigger("isAttacking");
}
}
//follow
if (direction.magnitude < visDist && angle < visAngle)
{
}
else
{
if (state != "IDLE")
{
state = "IDLE";
anim.SetTrigger("isIdle");
}
}
if (state == "RUNNING")
{
this.transform.Translate(0,0, speed * Time.deltaTime);
}
}
//patrol demon
private void PatrolGozu()
{
//Check if we're close to the destination
if (travelling && navMesh.remainingDistance <= 1.0f)
{
travelling = false;
//fi we're going to wait, then wait
if (patrolWaiting)
{
waiting = true;
waitTimer = 0f;
}
else
{
ChangePatrolPoint();
SetDestination();
}
}
if (waiting)
{
waitTimer += Time.deltaTime;
if (waitTimer >= totalWaitTime)
{
waiting = false;
ChangePatrolPoint();
SetDestination();
}
}
}
}
I know that the cone of vision of the demon it's working, but when patroling it does not stop to follow the player and continues to patrol between the points.
I'm sure I'm missing something, and since I'm relatively new to Unity right now I'm clueless.
Comment