2D TopDown, animations bug AI

Hi, thanks for stopping by, i set up a AI, whose function is to follow around the player. It’s working, the only problem is the animations. It’s bugged, I tried it all, but the AI only does 1 animation. All the code is down below.

Ai controller:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rita_AI : MonoBehaviour
{
    public float speed;
    public float checkRadius;
    public bool shouldrotate;
    public LayerMask WhatIsPlayer;
    private Transform target;
    private Rigidbody2D rb;
    private Animator animator;
    private Vector2 movement;
    public Vector3 dir;
    private bool isInChaseRange;
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        target = GameObject.FindWithTag("Player").transform;
    }
    private void Update()
    {
        animator.SetBool("isRunning", isInChaseRange);
        isInChaseRange = Physics2D.OverlapCircle(transform.position, checkRadius, WhatIsPlayer);
        dir = target.position - transform.position;
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        dir.Normalize();
        movement = dir;
        if (shouldrotate)
        {
            animator.SetFloat("X", dir.x);
            animator.SetFloat("Y", dir.y);
        }
    }
    private void FixedUpdate()
    {
        if (isInChaseRange)
        {
            MoveCharacter(movement);
        }
    }
    private void MoveCharacter(Vector2 dir)
    {
        rb.MovePosition((Vector2)transform.position + (dir * speed * Time.deltaTime));
    }
}

This is how i set up the Animator: Imgur: The magic of the Internet

The transition idle → blend tree has nothing
The transition blend tree → idle has a condition (isRunning = false)

Probably more important than the basic state graph will be the transition conditions, as well as any properties such as exit time, looping, etc.

I suggest going through your Animator graph and verifying the transitions are set up properly, and if they are, then verify the code sets the appropriate properties that those transitions expect.

If you do that and it still doesn’t work, time to debug 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

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 put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

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.

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://discussions.unity.com/t/839300/3

If ALL of that fails, set this aside and work through a basic MecAnim tutorial until you grasp the relationship between code, the properties, and how to properly configure an Animator.

Thank you so much, for real :slight_smile: god bless u dud :slight_smile: