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);
}