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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Crouch - Animator Doesn't Play Full Animation

Discussion in '2D' started by JayArtist, Apr 5, 2023.

  1. JayArtist

    JayArtist

    Joined:
    Jul 1, 2014
    Posts:
    89
    Hi,
    I'm trying to implement a Crouch control for a platformer. My bool variables appear to be working correctly, but the animation only plays the first frame. I'm obviously not using Exit Time on the Animator as I want the animation to play immediately as it's a 2D game. I've tried various methods in my script, but the frames just don't play through.

    The other scripts I have don't effect the Crouch script, as the problem persists even when they are disabled (I'm keeping my scripts seperate for future reference and modularity).

    I want the player to stay crouched whilst the button is pressed, then when released the crouching is cancelled and it goes back to the Idle animation.





    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerCrouch : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public Animator animator;
    10.  
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         // Crouch True
    16.         if (Input.GetButtonDown("Crouch"))
    17.         {
    18.             Debug.Log("True");
    19.             animator.SetBool("isCrouching", true);
    20.  
    21.         }
    22.  
    23.         // Crouch False
    24.         if (Input.GetButtonUp("Crouch"))
    25.         {
    26.             Debug.Log("False");
    27.             animator.SetBool("isCrouching", false);
    28.  
    29.         }
    30.  
    31.  
    32.     }
    33.  
    34.  
    35. }
     
  2. ilizzium

    ilizzium

    Joined:
    Aug 3, 2022
    Posts:
    2