Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why does the "Walk" state stop when the "Gun" state runs in Unity?

Discussion in 'Animation' started by Dan-100, Sep 26, 2023.

  1. Dan-100


    Apr 23, 2023
    I spent hours trying to solve this problem: when I run the game, the character is moving correctly to the right and left, but when pulling the gun happens, the character never moves for some reason.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class WalkParent : MonoBehaviour
    6. {
    7.     private Animator animator = null;
    8.     private Rigidbody2D rb;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         animator = GetComponent<Animator>();
    13.         rb = GetComponent<Rigidbody2D>();
    14.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         float dirX = Input.GetAxisRaw("Horizontal");
    20.         rb.velocity = new Vector2(7f * dirX, rb.velocity.y);
    22.         if (Input.GetButtonDown("Jump"))
    23.         {
    24.             rb.velocity = new Vector2(rb.velocity.x, 7f);
    25.         }
    26.         if (dirX > 0 || dirX < 0)
    27.         {
    28.             animator.SetBool("isWalking", true);
    29.         }
    30.         if (dirX == 0)
    31.         {
    32.             animator.SetBool("isWalking", false);
    33.         }
    35.         if (Input.GetButtonDown("Fire1"))
    36.         {
    37.             animator.SetTrigger("pull");
    38.         }
    39.         if (Input.GetButtonDown("Fire2"))
    40.         {
    41.             animator.SetTrigger("nopull");
    44.         }
    45.     }
    46. }

    I trigger the "Gun" animation with
    and it's just pulling the gun animation.

    This is how the problem looks:

    I thought it'd be easier to connect "Any State" to "Gun." But what I had tried for the first time was using "Bool" for all of the animations and then connecting them together without using "Any State," and the problem still happened. I don't know if it's because "Gun" and "Walk" have the same keyframes for bones, so that creates this problem for me.
  2. halley


    Aug 26, 2013
    Only one state on a layer is active at any given time. You're working with just one layer here.

    For each layer, there is only one "current state" and transitions are how to change the current state to a new state, making it active. The "Any State" is just a way to keep the diagram pretty, it basically represents an arrow from every state on the layer going to one place all with the same transition conditions.

    If you want to handle overlapping states, such as allowing your hands to do things without stopping the body movement as a whole, look into adding a new layer in the left side of your Animator window, and then assigning a mask to the new layer which tells it which parts are fair game to animate and which parts should be left to other layers.