0

Instead of animating a character in an inactive(Idle) state.

The B key is pressed here:.

After a while the animation works again:.

Exit time is disabled for transitions.

Player Controller file code:

public class PlayerController : MonoBehaviour
{
    public float moveSpeed;
    public Animator animator;
    Vector2 movement;
    private Rigidbody2D rb;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        animator.SetFloat("Horizontal", movement.x);
        animator.SetFloat("Vertical", movement.y);
        animator.SetFloat("Speed", movement.sqrMagnitude);
    }
    
    void FixedUpdate() 
    {
        
        rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime);   
    }
}

Horizontal and Vertical parameters: Animator Window Animator Window

Here are the transitions between the idle state and the running state (Movement): Animator Transition Idle -> Movement Animator Transition Movement-> Idle

After the speed parameter becomes greater than 0.01, the character goes into the running state(Movement), if less than 0.01, back to the idle state.

Nech
  • 3
  • 2

1 Answers1

0

Your question is not very clear. Please try to rephrase it. I don't see anything in code that changes state on button press. Where are you using the "Horizontal" and "Vertical" parameters?

May be this Getting started with Unity Animation guide will help you.

Vionix
  • 570
  • 3
  • 8
  • I added screenshots where the vertical and horizontal parameters are used. When I press and hold the button, there should be a transition from the idle state to the running animation. But sometimes, instead, the character moves, but remains in the idle state (as in the screenshot "The B key is pressed here:"). That is, the running animation does not work, but the character moves. Thank you for your attention to my question. – Nech Jun 04 '23 at 15:27
  • I don't see a code or parameter for transition on button press from Idle to movement. – Vionix Jun 05 '23 at 02:14
  • I added information above to clarify how the transition occurs in the animator. It all depends on the speed parameter. – Nech Jun 05 '23 at 07:42
  • The condition for Idle to movement is set to Speed less than 0.01. It should be greater than 0.01 – Vionix Jun 06 '23 at 02:03
  • Oh...Most likely this was the problem, but I just decided to write all the animation transitions in the code. Thank you for your attention to my problem, I will be more attentive next time. – Nech Jun 07 '23 at 13:54