Question by
andersonsacani93 · Apr 23 at 01:49 AM ·
ainavmeshagentpatrolmonster
Monsters are walking around without my permission, why?
I'm creating script to control an artificial intelligence, but the monsters are walking automatically. When I set the monster to patrol, then it patrols normally, but if I set it to not patrol, the monster is walking around randomly. I want the monster to stay still when the "patrollingCreature" option is set to "false".
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.AI;
public class AIController : MonoBehaviour
{
[SerializeField] private GameObject player;
[SerializeField] private Animator anim;
[SerializeField] private Transform[] waypoints;
private NavMeshAgent agent;
private float distance;
private bool isPlayerClose;
private int destPoint = 0;
public bool patrollingCreature = false;
public bool thisCreatureAttack = false;
public bool giveUpTheFight = true;
public float maxHitPoints = 3f;
public float currentHitPoints = 3f;
public float fleeingHitPoints = 1f;
public float maxAllowedDistance = 10f;
public float attackRange = 5f;
public float attackDamage = 10f;
public float walkSpeed = 10.34f; //5.335f
public float runSpeed = 31.04f;
void Awake()
{
anim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
}
void Start()
{
// 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;
}
void Update()
{
distance = Vector3.Distance(transform.position, player.transform.position);
if (distance <= maxAllowedDistance)
{
isPlayerClose = true;
}
else
{
isPlayerClose = false;
}
if (isPlayerClose == true)
{
if (thisCreatureAttack == true)
{
ToChase();
}
else
{
if (giveUpTheFight == true)
{
if (currentHitPoints <= fleeingHitPoints)
{
ToRun();
}
}
}
}
else
{
if (patrollingCreature == true)
{
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
{
ToPatrol();
}
}
else
{
ToRest();
}
}
}
private void ToRest()
{
agent.destination = transform.position;
agent.velocity = Vector3.zero;
}
private void ToPatrol()
{
agent.speed = walkSpeed;
// Returns if no points have been set up
if (waypoints.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = waypoints[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % waypoints.Length;
}
private void ToLook()
{
transform.LookAt(player.transform.position);
}
private void ToChase()
{
ToLook();
if (distance <= attackRange)
{
agent.velocity = Vector3.zero;
ToAttack();
}
else
{
agent.speed = runSpeed;
agent.destination = player.transform.position;
}
}
private void ToAttack()
{
///Attack the player every second, dealing damage determined on variables
}
private void ToRun()
{
///Run in the opposite direction of the player. Stay at a distance of 20f
}
}
Comment
Your answer
Follow this Question
Related Questions
NavMeshAgent patrol doesn't work 0 Answers
How do I get my guard(s) to stop at NavPoint(s) and go into IdleState in C# 0 Answers
Rigidbody like movement for Vector3.MoveTowards Nav 0 Answers
ai patrol and chase question 0 Answers
why does my agent keep circling around a previsoulsy set destination? 0 Answers