NavMeshAgent patrol doesn't work
So I'm making a 3D game for Brakeys game jam 2020. Right now I have a basic scene with an AI (right now a square). It has NavMeshAgent attached, NavMesh is propperly set up. The AI is controlled through a state machine and is supposed to patrol between 4 positions which are stored in EnemyController script. But instead it goes to the first position, stops and just stays there. I have no idea why this is happening please hjelp
NavMeshAgent
What happens
Script to store stuff
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyController : MonoBehaviour
{
#region Singleton
public static EnemyController instance;
void Awake()
{
instance = this;
}
#endregion
public NavMeshAgent agent;
public Transform[] patrollSpots;
public Transform spotParent;
int spotCount;
void Start()
{
agent = GetComponent<NavMeshAgent>();
spotCount = spotParent.childCount;
}
}
Patrol script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PatrollBehaviour : StateMachineBehaviour
{
//Transform playerPos;
Transform transform;
Transform spotParent;
public Transform[] patrollSpots;
int currentSpot = 0;
NavMeshAgent agent;
// Start
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
//playerPos = PlayerManager.instance.player.transform;
agent = EnemyController.instance.agent;
transform = EnemyController.instance.transform;
patrollSpots = EnemyController.instance.patrollSpots;
spotParent = EnemyController.instance.spotParent;
}
// Update
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
agent.SetDestination(patrollSpots[currentSpot].position);
if(Vector3.Distance(transform.position, patrollSpots[currentSpot].position) < 0.2f)
{
if(currentSpot >= spotParent.childCount - 1)
{
currentSpot = -1;
}
currentSpot++;
}
}
// Exit
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
}
// OnStateMove is called right after Animator.OnAnimatorMove()
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that processes and affects root motion
//}
// OnStateIK is called right after Animator.OnAnimatorIK()
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that sets up animation IK (inverse kinematics)
//}
}
Your answer
Follow this Question
Related Questions
How do I get my guard(s) to stop at NavPoint(s) and go into IdleState in C# 0 Answers
Monsters are walking around without my permission, why? 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