Search Unity

Question 2D Animation, Unsure what I am doing wrong

Discussion in 'Animation' started by Nimeode, Dec 3, 2022.

  1. Nimeode

    Nimeode

    Joined:
    May 21, 2018
    Posts:
    10
    Still figuring things out, been researching for a while on why an animation is not starting. Below is the code that is being used
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerTestScript : MonoBehaviour
    6. {
    7.     public float moveSpeed;
    8.     public float horizontalMovement;
    9.     private Rigidbody2D rigidBody2d;
    10.     private Animator anim;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rigidBody2d = GetComponent<Rigidbody2D>();
    16.         anim = GetComponent<Animator>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         horizontalMovement = Input.GetAxisRaw("Horizontal");
    23.         rigidBody2d.velocity = new Vector2(horizontalMovement * moveSpeed, rigidBody2d.velocity.y);
    24.         //Walk Animation
    25.         if (horizontalMovement == 0)
    26.         {
    27.             anim.SetBool("isWalking", false);
    28.             if (anim.GetBool("isWalking"))
    29.             {
    30.                 Debug.Log("isIdle");
    31.             }
    32.         }
    33.         else
    34.         {
    35.             anim.SetBool("isWalking", true);
    36.             if (anim.GetBool("isWalking"))
    37.             {
    38.                 Debug.Log("isWalking");
    39.             }
    40.         }
    41.     }
    42. }
    43.  
    I am able to see with the log that the bool is getting set to true, as well as in the animator (GIF)

    Unity_7pvFpfIjDZ.gif

    Any thoughts or advice would be appreciated.
     
  2. Nostra88

    Nostra88

    Joined:
    Oct 13, 2022
    Posts:
    5
    The code is working. Im not seeing the transition from idle to walk lighting up though. the transition from walk to idle however does. Did you just forget to add the parameter to the transition possibly?
     
  3. Nimeode

    Nimeode

    Joined:
    May 21, 2018
    Posts:
    10
    So I just found out the issue about an hour ago. The issue came about because of where the animator component was in the hierarchy.

    the animator itself was on the sprite while the component was on the main object (if that makes sense)