Support Forum

AI walking in circle

Hello,

I just started using this asset today and I made a simple AI using the Behavior designer asset to make the AI wander around… the problem is the AI walks in circle never reaching the destination…

A video of the problem:

This happens when the AI reaches the destination

These are the components

I also have a custom script attached to the AI that uses:

    void OnAnimatorMove()
    {
        //only perform if walking
        if (moveState != MoveState.Idle)
        {
            //set the navAgent's velocity to the velocity of the animation clip currently playing
            aIPath.Move(animController.deltaPosition);
        }
    }

I don’t think this is the cause because I removed it and I still had the same problem.

Hi

Do you perhaps have root motion enabled on your animation controller? That would cause the agent to move forward more than it expected and fail to turn around in time.

ok so i found what is causing this… but im not sure how to fix it

using UnityEngine;
using BehaviorDesigner.Runtime;
using Pathfinding;

public enum MoveState { Idle, Walking, Run}

public class AIAnimations : MonoBehaviour
{
    [ReadOnly, SerializeField] private MoveState moveState;

    [Space(10)]
    [SerializeField] private Animator animController;
    [SerializeField] private AIPath aIPath;
    [SerializeField] private BehaviorTree agent;

    [Space(10)]
    [SerializeField] private float walkSpeed = 1.5f;
    [SerializeField] private float runSpeed = 3f;

    void Update()
    {
        var danger = (SharedBool)agent.GetVariable("Flee");

        //character walks if there is a navigation path set, idle all other times
        if (aIPath.hasPath && !danger.Value)
            moveState = MoveState.Walking;
        else if (aIPath.hasPath && danger.Value)
            moveState = MoveState.Run;
        else
            moveState = MoveState.Idle;


        if (moveState == MoveState.Run)
        {
            animController.SetFloat("Speed", runSpeed);
        }
        else
        {
            animController.SetFloat("Speed", walkSpeed);
        }

        //send move state info to animator controller
        animController.SetInteger("MoveState", (int)moveState); // <-- this is what is causing the problem..
    }

    void OnAnimatorMove()
    {
        //only perform if walking
        if (moveState != MoveState.Idle)
        {
            //set the navAgent's velocity to the velocity of the animation clip currently playing
            aIPath.Move(animController.deltaPosition);
        }
    }
}

whenever the script changes the animation it causes it

I would recommend that you do not use this line. It will confuse the agent because it wants to do its own movement.

I use this line to make the animation look better its not really a problem… the problem is with the line I mentioned I pointed it out

//send move state info to animator controller
        animController.SetInteger("MoveState", (int)moveState); // <-- this is what is causing the problem..

This line is the problem but not sure how to switch between animations without causing this problem

Hi

The line you pointed out only changes what animations play. But it doesn’t directly influence the movement.

This one

aIPath.Move(animController.deltaPosition);

does influence movement, however. Which is why I think it’s the problem. If not this line, then you have some other code that uses animation data to drive the agent’s position and/or rotation.

ok so I disabled RootMotion and it fixed the problem… Thanks!

and another question… is the MaxSpeed works as like a current speed for an example if an enemy is chasing the AI and I increased the max speed is it going to be faster? because now I need to change speed from your system :slight_smile:

Yes, the agent will quickly accelerate up to the maximum speed if you change it.

1 Like