Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question (SOLVED) Navmesh Agent not moving right

Discussion in 'Editor & General Support' started by bendelehezekiah, Jan 23, 2023.

  1. bendelehezekiah

    bendelehezekiah

    Joined:
    Jan 23, 2023
    Posts:
    2
    I am trying to make a nav mesh agent go to a point, and when that point changes, head to that new location. At first it wouldn’t move at all if the point was too far, so I set up a system to target a position a set amount of distance away, “maxDistance,” and if it is beyond that distance move in that direction. This gave me an issue as well, with the only thing helping out is to offset the point on the y axis, and now finally where it half works, but also doesn't.



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class AiPlayer : MonoBehaviour
    7. {
    8.     [SerializeField] private Transform moveTarget;
    9.     [SerializeField] private float targetDistance;
    10.     [SerializeField] private float maxDistance = 280f;
    11.     [SerializeField] private Transform indecater;
    12.     [SerializeField] private Vector3 offset = new Vector3(0, 0.25f, 0);
    13.     [SerializeField] private NavMeshAgent navMeshAgent;
    14.  
    15.     private void Awake()
    16.     {
    17.         navMeshAgent = GetComponent<NavMeshAgent>();
    18.     }
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.        
    24.     }
    25.  
    26.     Vector3 lastTarget = Vector3.zero;
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         moveTarget.position = DataManager.main.alertPosition;
    31.         if (moveTarget.position != lastTarget) { NewDestination(); }
    32.      
    33.  
    34.     }
    35.  
    36.     void NewDestination()
    37.     {
    38.         targetDistance = Vector3.Distance(transform.position, moveTarget.position);
    39.         if (moveTarget.position != Vector3.zero)
    40.         {
    41.             if (targetDistance < maxDistance)
    42.             {
    43.                 navMeshAgent.destination = moveTarget.position;
    44.                 indecater.position = moveTarget.position;
    45.             }
    46.             else
    47.             {
    48.                 Vector3 _newTarget = DestinationDirection(moveTarget.position);
    49.                 navMeshAgent.destination = _newTarget;
    50.                 indecater.position = _newTarget;
    51.             }
    52.         }
    53.     }
    54.  
    55.     Vector3 DestinationDirection(Vector3 targetPosition)
    56.     {
    57.         RaycastHit hit;
    58.         Vector3 _out = transform.position + (targetPosition - transform.position).normalized * maxDistance;
    59.         _out += new Vector3(0,30,0);
    60.         if (Physics.Raycast(_out,Vector3.down, out hit))
    61.         {
    62.             _out = hit.point + offset;
    63.         }
    64.         return _out;
    65.     }
    66. }
    67.  
     
  2. bendelehezekiah

    bendelehezekiah

    Joined:
    Jan 23, 2023
    Posts:
    2
    I… uuuhh… “fixed” it. Found out the main issue is the point I was setting as the destination was inside a navmesh obstacle. I “wiggle” the position of the destination in cases when the target was too far away. Before the raycast I just added
    Code (CSharp):
    1.  
    2. _out += new Vector3(Random.Range(wiggleOffset.x, wiggleOffset.y), 0, Random.Range(wiggleOffset.x, wiggleOffset.y));
    3.  
    4.  
    I am aware this is not the “best” option, but I am going with it. Also, I did in fact spot that I was running it every frame, now I run it at a set number of frames.