c# – Resetting inputs while still pressing down keys in Unity3D?
I have a character controller with no rigidbody or collider, and I’m trying to reset my input keys (old input system) when a certain condition turns true. For example if I am running while simultaneously holding down the jump key so W and Space and then a specific condition turns true, despite still holding down W and Space, they will “reset” and my player will stop moving until I release and then press those keys again. Or if I’m pressing W on its own, or Space on its own.
I made a very simple controller script to use to test this, and I’ve tried wrapping a bool around the code called isMoving and then if another bool such as isSliding turns true, then isMoving turns false which would prevent movement, but with how I did it, no keys reset. I also tried to use a Raycast that casted forward which would also turn isMoving false if it hit a specific collider, but I could still hold down those input keys and perform the same action.
void Update()
{
isGrounded = controller.isGrounded;
if (isGrounded)
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
float playerSpeed = Input.GetKey(KeyCode.LeftShift) ? sprintingSpeed : walkingSpeed;
Vector3 forward = transform.forward * z;
Vector3 right = transform.right * x;
move = (forward + right).normalized * playerSpeed;
if (Input.GetButtonDown("Jump"))
{
move.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
}
move.y += gravity * Time.deltaTime;
controller.Move(move * Time.deltaTime);
}
Read more here: Source link
