Search Unity

Question Animation jerks when navmesh agent sets a new path

Discussion in 'Scripting' started by IamKevinRichardson, Nov 28, 2022.

  1. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Hello everyone!

    I have a problem where my AI character jerks every time the navmesh agent finds a new path.

    The navemesh agent is following the player and other AI targets so I have it update the path about every 2 seconds.

    I assume the jerking happens because the navmesh velocity is set to 0 every time a new path is made?

    Anyway, I am hoping someone can point me in the right direction in order to fix this.

    Here is the code I use to control the animations...

    Code (CSharp):
    1.     void ControlLocomotion()
    2.     {
    3.         //locomotion
    4.         worldDeltaPosition = nav.nextPosition - transform.position;
    5.         groundDeltaPosition.x = Vector3.Dot(transform.right, worldDeltaPosition);
    6.         groundDeltaPosition.y = Vector3.Dot(transform.forward, worldDeltaPosition);
    7.         velocity = (Time.deltaTime > 1e-5f) ? groundDeltaPosition / Time.deltaTime : velocity = Vector2.zero;
    8.         anim.SetFloat("velx", velocity.x);
    9.         anim.SetFloat("vely", velocity.y);
    10.     }
    If anyone has a better code that they are willing to share I would also appreciate it.

    I also tried unity's code here Unity - Manual: Coupling Animation and Navigation (unity3d.com)

    but it has the same issue and the code makes the AI "vibrate" as opposed to the code that I posted.
     
    Last edited: Nov 28, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    First prove that this is what is happening.

    If it is, then a little piece of code to prevent sending a zero speed through unless the zero speed persists for more than a frame or two might be all you need.

    But prove what the problem is first or you are chasing ghosts.

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

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    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.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    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, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    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

    When in doubt, print it out!(tm)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Oh no I am going to have to debug it! lol

    I was just hoping it was a more common problem that someone already had a solution for before I spend a bunch of energy on it. I tend to "reinvent the wheel" a lot and I am trying to avoid it when possible.

    Thank you for your response.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Easy enough:

    - strip everybody out except one guy

    - spam the speeds to the log

    - run the game until you see the problem

    - study the log

    that way you don't waste time trying to fix what it isn't
     
  5. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Yep it is setting the velocity to 0

    Debug Log.jpg



    Does unity have a built in fix for this or will I have to store the velocity before SetDestination and pass it through a few frames? I can't find anything in the documentation.

    Again, I am tired of writing scripts for things that could easily be fixed by just checking the right box somewhere.

    Edit: I got it set up with bools and timers and all that stuff but if anyone knows a better way please let me know.
     
    Last edited: Nov 29, 2022
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I'm looking at your code and wondering why you are computing velocity that way?

    Why not observe the position of the transform this frame vs last frame and use that delta to compute your velocity?

    You're doing something with .nextPosition and I have no idea what that property is or what it might do. That alone could be the source of your problems because if you watch a navmesh agent when you give it a new destination, it doesn't physically slow down assuming it needs to keep going in the same direction.
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    You're in complete control over when you call anim.SetFloat. Just don't do it when you don't want the animation to change.
     
  8. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Bah humbug!
    Now my AI has jerkiness in other instances that it did not have before. I feel like I am wasting my time plugging these holes when there has to be a better solution.

    Tried this using:

    Code (CSharp):
    1.         worldDeltaPosition = transform.position - previousPosition;
    2.         previousPosition = transform.position;
    in both fixed update and normal update. Still has the jerky behavior... =(
     
  9. IamKevinRichardson

    IamKevinRichardson

    Joined:
    Feb 5, 2014
    Posts:
    52
    Code (CSharp):
    1. NavMeshPath path = new NavMeshPath();
    2.             if (NavMesh.CalculatePath(transform.position, objective.transform.position, NavMesh.AllAreas, path))
    3.             {
    4.                 nav.SetPath(path);
    5.             }
    Boom! Finally found it. I knew I didn't have to do all that lame debugging lol.
     
    MattThomSA likes this.