Search Unity

Ai moving out of itself

Discussion in 'Navigation' started by Deleted User, Apr 13, 2022.

  1. Deleted User

    Deleted User

    Guest

    I have this thing where a thin wall(image) is chasing you. Or better said, is supposed to chase you when you get into his look radius. However, the AI moves out of itself and just runs into the wall. And when I get in his look range he doesn't do anything.

    It Happening:

    Settings on the Game Object are attached

    The AI script:
    Code (CSharp):
    1. using UnityEngine.AI;
    2. using UnityEngine;
    3.  
    4. public class AI : MonoBehaviour
    5. {
    6.     public float lookRadius = 50f;
    7.  
    8.     Transform target;
    9.     NavMeshAgent agent;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         target = PlayerManager.instance.transform;
    16.         agent = GetComponent<NavMeshAgent>();  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         float distance = Vector3.Distance(target.position, transform.position);
    23.  
    24.         if(distance <= lookRadius)
    25.         {
    26.             agent.SetDestination(target.position);
    27.         }
    28.     }
    29.  
    30.     void OnDrawGizmosSelected()
    31.     {
    32.         Gizmos.color = Color.red;
    33.         Gizmos.DrawWireSphere(transform.position, lookRadius);
    34.     }
    35. }
    36.  
    Help would be appreciated
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Obviously the first visual check is to make sure all your nav AI stuff is baked properly. If it is, move onto the code.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.