Patrolling makes the NPC have Jittery movements

I’m trying to make a simple patrol script for an NPC but it seems whatever I do I end up with jittery movements when rotating.

Here is the pathfinder:

And here is characters RichAI:

Now here is the script I’m using for patrolling but do note it’s just a testing script since I want to test this pathfinding since navmesh is not baking anymore…Basically I tried every option under the Sun like setting isStopped = true, setting destination to itself but the only thing that worked for making the character stop without jittering at the waypoint was just disabling the richAI(Let me know if there is a better option):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;

namespace LastIsekai
{
public class TestNPC : MonoBehaviour
{
public List waypoints;
public float speed = 3f;
public float nextWaypointDistance = 1f;
public float waypointDelay = 2f; // Adjust this variable for the delay at each waypoint

    private RichAI richAI;
    private int currentWaypoint = 0;
    private bool isWaiting = false;
    private float waitTimer = 0f;

    private Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
        richAI = GetComponent<RichAI>();
        SetNextWaypoint();
    }

    void SetNextWaypoint()
    {
        if (waypoints.Count > 0)
        {
            richAI.destination = waypoints[currentWaypoint].position;
        }
    }

    void Update()
    {
        if (isWaiting)
        {
            waitTimer += Time.deltaTime;
            animator.SetFloat("Speed", 0f); // Set speed to 0 when waiting

            if (waitTimer >= waypointDelay)
            {
                isWaiting = false;
                currentWaypoint++;
                waitTimer = 0f;

                if (currentWaypoint >= waypoints.Count)
                    currentWaypoint = 0; // Loop back to the first waypoint

                SetNextWaypoint();
                richAI.enabled = true; // Enable RichAI after the delay
            }
        }
        else
        {
            if (Vector3.Distance(transform.position, richAI.destination) < nextWaypointDistance)
            {
                isWaiting = true; // Start waiting when reaching the waypoint
                richAI.enabled = false; // Disable RichAI during the delay
            }
        }
    }

    private void LateUpdate()
    {
        // Set the speed parameter for animation
        animator.SetFloat("Speed", 1f);
    }
}

}

So yeah I tried fiddling with settings for hours and I can’t seem to understand why it jitters…Also I tried the official Patrol.cs from the documentation I found and that jittered badly also…Components I have on the Player are: Animator for animation and this rigidbody:

image

Here is the link to the video showing the jittering…

link to video

I should note that it didn’t jitter when it was just continously going from one waypoint to another without the stopping at the waypoint…I think that happened because when it got to a distance of <= 1 of the waypoint it would get another destination thus never actually arriving at the waypoint which I think causes this jitter

Hi

It looks like the video you sent requires me to log in. Is it possible to send it in some other way?

Hi

I can’t see anything obvious.
Do you have root motion enabled on the animator?
Try disabling that, as it can cause a lot of interference.

Also. I would recommend that you use richAI.isStopped instead of disabling the component.

I smoothened that rotation glitching when it’s going to next waypoint it by adding Simple Smooth component with Bezier. Just hope it won’t affect performance too much and I also disabled root motion

Thanks

1 Like