I wanted to create a crouch toggle for my character, so that when I press a key my character will stay in the crouch animation.
However when I try this with a bool, the bool simply keep flickering when I press the assigned key
…which is quite amusing but not very helpful
This is the part of the codes that handles crouching animation
// Update is called once per frame
void Update()
{
bool isrunning = animator.GetBool("isRunning");
bool isWalking = animator.GetBool("IsWalking");
bool isCrouching = animator.GetBool("isCrouching");
bool isBackwards = animator.GetBool("isBackwards");
bool forwardPressed = Input.GetKey("w");
bool runPressed = Input.GetKey("left shift");
bool crouchpressed = PlayerMovement.isCrouching;
bool backwardPressed = Input.GetKey("s");
// Toggle Crouch
if (crouchpressed && !isCrouching)
{
animator.SetBool("isCrouching", true);
}
else if (crouchpressed && isCrouching)
{
animator.SetBool("isCrouching", false);
}
And here is the part in the character controller that lets the animator knows if my character is crouching
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private CharacterController controller;
public float speed = 5f;
public float runSpeed = 10f;
public float crouchSpeed = 1f;
public float backwardsSpeed = 2f;
public float crouchBackwardsSpeed = 1f;
public float turnSpeed = 180f;
public bool isCrouching = false;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftControl) && !isCrouching)
{
isCrouching = true;
Debug.Log(isCrouching);
}
else if (Input.GetKeyDown(KeyCode.LeftControl) && isCrouching)
{
isCrouching = false;
Debug.Log(isCrouching);
}
Have been stuck for a few days, decide to seek help in case it is something very obvious