Here’s My Code:
using UnityEngine;
public class Player_Crouch : MonoBehaviour
{
private CharacterController m_CharacterController;
public KeyCode crouchKey = KeyCode.C;
public KeyCode jumpKey = KeyCode.Space;
private bool m_Crouch = false;
private float m_OriginalHeight;
[SerializeField] private float m_CrouchHeight = 0.9f;
void Start()
{
m_CharacterController = GetComponent<CharacterController>();
m_OriginalHeight = m_CharacterController.height;
}
void Update()
{
if (Input.GetKeyDown(crouchKey))
{
m_Crouch = !m_Crouch;
CheckCrouch();
}
}
void CheckCrouch()
{
if (m_Crouch == true)
{
m_CharacterController.height = m_CrouchHeight;
}
else if (Input.GetKeyDown(crouchKey) && m_CharacterController.height == m_CrouchHeight)
{
m_CharacterController.height = Vector3.Lerp(m_CrouchHeight, m_OriginalHeight, Time.deltaTime);
}
}
}
The problem line is (43,57) and (43,73). Basically, I want it that if I press the “crouch” button when I’m in a crouching position, I go back to my original height over time instead of “snapping” to my original height.