Need help with NPC animation

Hello, ive been trying to make my npcs walk to waypoints and use the animations ive made. I dont know what im doing wrong but its not working.

heres the script:

using System.Collections;
using UnityEngine;

public class LoganMovement : MonoBehaviour
{
    public float moveSpeed = 1f;
    public Animator animator;
    public Vector2[] waypoints; // Public array to set different waypoints in the Inspector

    private int currentWaypointIndex = 0;
    private bool isMoving = false;

    void Start()
    {
        if (waypoints.Length > 0)
        {
            StartCoroutine(MoveAlongPath());
        }
        else
        {
            Debug.LogWarning("No waypoints set for Logan.");
        }
    }

    IEnumerator MoveAlongPath()
    {
        while (true)
        {
            if (!isMoving && waypoints.Length > 0)
            {
                Vector2 targetPos = waypoints[currentWaypointIndex];
                StartCoroutine(MoveToTarget(targetPos));
                currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
            }
            yield return null;
        }
    }

    IEnumerator MoveToTarget(Vector2 targetPos)
    {
        isMoving = true;

        while ((Vector2)transform.position != targetPos)
        {
            Vector2 newPos = Vector2.MoveTowards((Vector2)transform.position, targetPos, moveSpeed * Time.deltaTime);
            UpdateAnimation(newPos - (Vector2)transform.position);
            transform.position = newPos;
            yield return null;
        }

        UpdateAnimation(Vector2.zero); // Stop animation
        isMoving = false;
    }

    void UpdateAnimation(Vector2 direction)
    {
        if (direction != Vector2.zero)
        {
            animator.SetFloat("MoveX", direction.x);
            animator.SetFloat("MoveY", direction.y);
        }
        animator.SetBool("IsMoving", direction != Vector2.zero);
    }

Hello @nikolishh,

First you should try it without the animation part, without the variables and test directly in the Update function to see if nothing messes up with your character, try this to see if your npc moves or not:

private void Update() {
    Vector2 newPos = Vector2.MoveTowards((Vector2)transform.position, waypoints[1], 5 * Time.deltaTime);
    transform.position = newPos;
}

Then check if you have a good number of waypoints at a good distance from eachother, check the moveSpeed, also when checking if your character arrived to the waypoint you should make a distance check because while ((Vector2)transform.position != targetPos) can fail due to floating points precision:

 (Vector2.Distance(transform.position, targetPos) > 0.01f)

I hope it helps

I see that your animator uses transitions from “Any State”. All transitions of this type include an additional parameter “Can Transition To Self”. If this box is checked, this means the animator will perform the transition to a state even if it is already in that state. This can cause the animation to restart from frame 0 every frame.

For state-based (non-trigger) transitions, you will usually want to uncheck “Can Transition To Self”.

i would usually have my idle animation at the center , using anyState you must be carefull, used when you absolutely know your states are well orginized, since anyState can fire, it becomes cumbersome to manage. just a side note if you already filled your inspector fields with waypoints, you dont have to check on start if the length is 0 or not. but you would do that if say you had to clear the list and get new waypoints if you need to later on… in your moveTarget function, you say “while transform.position!=targetPos”, be better with another function checking Distance to target

just a pseudo quick example:

Distance=Vector2.Distance(targetPos-transform.position)
if Distance >.2f
 {
goAnim

}
if Distance<=.2f
 {
stopAnim, or attack anim, etc...
// call function to do stuff if needed
}

hope it helps some!