Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved 2D TopDown, animations bug AI

Discussion in 'Scripting' started by Silvaantonio, Sep 7, 2021.

  1. Silvaantonio

    Silvaantonio

    Joined:
    Apr 29, 2021
    Posts:
    9
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Rita_AI : MonoBehaviour
    5. {
    6.     public float speed;
    7.     public float checkRadius;
    8.     public bool shouldrotate;
    9.     public LayerMask WhatIsPlayer;
    10.     private Transform target;
    11.     private Rigidbody2D rb;
    12.     private Animator animator;
    13.     private Vector2 movement;
    14.     public Vector3 dir;
    15.     private bool isInChaseRange;
    16.     private void Start()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         animator = GetComponent<Animator>();
    20.         target = GameObject.FindWithTag("Player").transform;
    21.     }
    22.     private void Update()
    23.     {
    24.         animator.SetBool("isRunning", isInChaseRange);
    25.         isInChaseRange = Physics2D.OverlapCircle(transform.position, checkRadius, WhatIsPlayer);
    26.         dir = target.position - transform.position;
    27.         float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
    28.         dir.Normalize();
    29.         movement = dir;
    30.         if (shouldrotate)
    31.         {
    32.             animator.SetFloat("X", dir.x);
    33.             animator.SetFloat("Y", dir.y);
    34.         }
    35.     }
    36.     private void FixedUpdate()
    37.     {
    38.         if (isInChaseRange)
    39.         {
    40.             MoveCharacter(movement);
    41.         }
    42.     }
    43.     private void MoveCharacter(Vector2 dir)
    44.     {
    45.         rb.MovePosition((Vector2)transform.position + (dir * speed * Time.deltaTime));
    46.     }
    47. }
    48.  
    This is how i set up the Animator: https://imgur.com/a/ZAMYdRJ

    The transition idle -> blend tree has nothing
    The transition blend tree -> idle has a condition (isRunning = false)
     
    Last edited: Sep 8, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    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://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    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.
     
  3. Silvaantonio

    Silvaantonio

    Joined:
    Apr 29, 2021
    Posts:
    9
    Thank you so much, for real :) god bless u dud :)