I added crouching to my movement script, what messing is to check if there is a ceiling above the player, and if there is the player should remain crouched. How would I achieve this?
currently, for crouching, the user should press the c key and if he wants to stand up, he should press it again.
UPDATED CODE:
if (crouching)
{
isRunning = false;
transform.localScale = new Vector3(transform.localScale.x, crouchYScale, transform.localScale.z);
movement = (move.y * transform.forward) + (move.x * transform.right);
controller.Move(movement * crouchSpeed * Time.deltaTime);
}
else
{
transform.localScale = new Vector3(transform.localScale.x, startYScale, transform.localScale.z);
movement = (move.y * transform.forward) + (move.x * transform.right);
controller.Move(movement * moveSpeed * Time.deltaTime);
}
controls.Player.Crouch.performed += _ => OnCrouch();
Here is my attempt:
public void OnCrouch()
{
RaycastHit hit;
bool checkCeiling = Physics.Raycast(transform.position, -Vector3.up, out hit, 2f);
if (crouching && checkCeiling == true)
{
crouching = false;
}
else
{
crouching = true;
}
}
But this does not show any result. the Player still can stand up even if there is a near ceiling above his head. How can I prevent standing up if there is a near ceiling and allow it only when there is enough space to stand up? please help.
