So, I'm having this problem that if my waypoints are a long way from each other, the NavMeshAgent freaks out. What's going on?
Here's my basic code:
Here's my basic code:
Code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class HunterAI : MonoBehaviour { NavMeshAgent myAgent; Animator myAnimator; public Transform[] wayPoints; public int nextWayPointNumber; Vector3 worldDeltaPosition = Vector3.zero; void Start() { CleanUpWPGeo(); myAgent = GetComponent<NavMeshAgent>(); myAnimator = GetComponent<Animator>(); myAnimator.SetBool("isWalking", true); myAgent.updatePosition = false; SetNewDestination(); } void Update() { if(myAgent.remainingDistance < .5f) { SetNewDestination(); } worldDeltaPosition = myAgent.nextPosition - transform.position; if(worldDeltaPosition.magnitude > myAgent.radius) { myAgent.nextPosition = transform.position + .9f * worldDeltaPosition; } } void SetNewDestination() { if(nextWayPointNumber >= wayPoints.Length) { nextWayPointNumber = 0; } myAgent.SetDestination(wayPoints[nextWayPointNumber].position); nextWayPointNumber++; } void OnAnimatorMove() { // Update position based on animation movement using navigation surface height Vector3 position = myAnimator.rootPosition; position.y = myAgent.nextPosition.y; transform.position = position; } void CleanUpWPGeo() { foreach(Transform wp in wayPoints) { wp.gameObject.GetComponent<MeshRenderer>().enabled = false; } } private void OnDrawGizmosSelected() { for(int i = 0; i< wayPoints.Length - 1; i++) { Gizmos.color = Color.green; Gizmos.DrawLine(wayPoints[i].position, wayPoints[i + 1].position); } Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, attackRange); Gizmos.color = Color.white; Gizmos.DrawWireSphere(transform.position, chaseRange); Gizmos.color = Color.green; Gizmos.DrawWireSphere(transform.position, gotAwayRange); } }
Comment