- Home /
How do I keep a crouch animation while pressing a key?
Hello everyone I have a question I made an animation of crouching but it only crouches for a second I have no idea how to keep it crouched while a button is pressed and when it is released it returns to the previous animation I appreciate if someone can help me
This is my code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Audio;
public class Jugador : MonoBehaviour { public float fuerzaSalto; public GameManager gameManager; public Score ScoreScript; //Reference this in the Hierarchy, like you did with the 'GameManager' script public Timer Timer;
 public Transform groundCheck; // se sabe cuando el jugador estatocando el suelo
 public LayerMask ground;
 public float groundCheckRadius;
 AudioSource jumpsound;
 public AudioSource Death;
 private Rigidbody2D rigidbody2D;
 private Animator animator;
 private bool isGrounded;
 public int playerJumps;
 private int tempPlayerJumps;
 // Start is called before the first frame update
 void Start()
 {
     animator = GetComponent<Animator>();
     rigidbody2D = GetComponent<Rigidbody2D>();
     jumpsound = GetComponent<AudioSource>();
 }
 // Update is called once per frame
 private void FixedUpdate()
 {
     isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
 }
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.DownArrow))
     {
         animator.SetBool("crouching", true);
     }
     if (isGrounded)
     {
         tempPlayerJumps = playerJumps;
     }
     if (Input.GetKeyDown(KeyCode.Space) && tempPlayerJumps > 0)
     {
         rigidbody2D.velocity = Vector2.up * fuerzaSalto;
         tempPlayerJumps--;
     }
     if (Input.GetKeyDown(KeyCode.Space))
     {
         jumpsound.Play();
         animator.SetBool("jumping", true);
         rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
     }
 }
 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "ground")
     {
         animator.SetBool("jumping", false);
     }
     if (collision.gameObject.tag == "obstacle")
     {
         gameManager.gameOver = true;
         ScoreScript.StopScore();
         Death.Play();
     }
     if (collision.gameObject.tag == "obstacle")
     {
         gameManager.gameOver = true;
         Debug.Log("gameOver");
         Timer.enabled = false;
     }
     if (collision.gameObject.tag == "ground")
     {
         animator.SetBool("crouching", false);
     }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Caeser_21 · Mar 17 at 05:54 PM
Instead of calling animator.SetBool("crouching", false); in "OnCollisionEnter()", call it like this :
 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine; 
 using UnityEngine.Audio;
 
 public class Jugador : MonoBehaviour 
 { 
  public float fuerzaSalto; public GameManager gameManager; 
  public Score ScoreScript; //Reference this in the Hierarchy, like you did with the 'GameManager' script 
  public Timer Timer;
 
  public Transform groundCheck; // se sabe cuando el jugador estatocando el suelo
  public LayerMask ground;
  public float groundCheckRadius;
  AudioSource jumpsound;
  public AudioSource Death;
  private Rigidbody2D rigidbody2D;
  private Animator animator;
  private bool isGrounded;
  public int playerJumps;
  private int tempPlayerJumps;
 
  void Start()
  {
      animator = GetComponent<Animator>();
      rigidbody2D = GetComponent<Rigidbody2D>();
      jumpsound = GetComponent<AudioSource>();
  }
 
  private void FixedUpdate()
  {
      isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, ground);
  }
  void Update()
  {
      if (Input.GetKeyDown(KeyCode.DownArrow))
      {
          animator.SetBool("crouching", true);
      }
      if (Input.GetKeyUp(KeyCode.DownArrow)) //This is where you call it instead
      {
          animator.SetBool("crouching", false);
      }
      if (isGrounded)
      {
          tempPlayerJumps = playerJumps;
      }
      if (Input.GetKeyDown(KeyCode.Space) && tempPlayerJumps > 0)
      {
          rigidbody2D.velocity = Vector2.up * fuerzaSalto;
          tempPlayerJumps--;
      }
      if (Input.GetKeyDown(KeyCode.Space))
      {
          jumpsound.Play();
          animator.SetBool("jumping", true);
          rigidbody2D.AddForce(new Vector2(0, fuerzaSalto));
      }
  }
  private void OnCollisionEnter2D(Collision2D collision)
  {
      if (collision.gameObject.tag == "ground")
      {
          animator.SetBool("jumping", false);
      }
      if (collision.gameObject.tag == "obstacle")
      {
          gameManager.gameOver = true;
          ScoreScript.StopScore();
          Death.Play();
      }
      if (collision.gameObject.tag == "obstacle")
      {
          gameManager.gameOver = true;
          Debug.Log("gameOver");
          Timer.enabled = false;
      }
      //if (collision.gameObject.tag == "ground") 
      //{
          //animator.SetBool("crouching", false); //Do not call this here
      //}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                