- Home /
This question was
closed Mar 28, 2020 at 12:52 PM by
Human_Melon for the following reason:
The question is answered, right answer was accepted
Question by
Human_Melon · Mar 27, 2020 at 05:54 PM ·
c#player movementkeypress1st person
Uncrouch on realese?
I need to Uncrouch on Key Release "C"
Here is the code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Crouch : MonoBehaviour {
private CharacterController m_CharacterController;
private bool m_Crouch = false;
private float m_OriginalHeight;
float speed = 5f;
[SerializeField] private float m_CrouchHeight = 0.5f;
public KeyCode crouchKey = KeyCode.C;
void Start()
{
m_CharacterController = GetComponent<CharacterController>();
m_OriginalHeight = m_CharacterController.height;
}
void Update()
{
if (Input.GetKeyDown(crouchKey))
{
m_Crouch = !m_Crouch;
CheckCrouch();
}
else if (Input.GetKeyUp(crouchKey))
{
// What do i do here??
}
}
void CheckCrouch() {
if (m_Crouch == true) {
m_CharacterController.height = m_CrouchHeight;
}
else {
m_CharacterController.height = m_OriginalHeight;
}
} }
Comment
Best Answer
Answer by Hellium · Mar 27, 2020 at 07:13 PM
void Update()
{
if (Input.GetKeyDown(crouchKey))
m_CharacterController.height = m_CrouchHeight;
else if (Input.GetKeyUp(crouchKey))
m_CharacterController.height = m_OriginalHeight;
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Will someone please help me??? 2 Answers
How to make an object move to another object's location in a 2d game? 2 Answers