I'm trying to create a crouch and prone system in my FPS game. I want the player to go from stand -> crouch or prone -> crouch when the player's current height is at the stand or crouch heights. The stand -> crouch works fine and I can toggle between them. The issue comes when I try to go from prone -> crouch and my character just stays prone. I can go from prone -> stand fine but I want to make it so that I can go from prone -> crouch -> stand from pressing the crouch button. I'm using the input system to try and accomplish this. This is everything I have in my script relating to this:
#Crouch and Prone System for FPS game
1 messages · Page 1 of 1 (latest)
!code
Posting code
📃 Large Code Blocks
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
also I would highly suggest you look into using an enum for this to make it less confusing
private enum PlayerState
{
Stand,
Crouch,
Prone,
Sprint
}
private PlayerState stance = PlayerState.Stand;
void Start()
{
currentSpeed = walkSpeed;
currentHeight = standingHeight;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void HandlePlayerHeight()
{
// Toggle crouch
if (playerInputHandler.CrouchTriggered)
{
if (stance == PlayerState.Stand)
stance = PlayerState.Crouch;
else if (stance == PlayerState.Prone)
stance = PlayerState.Crouch;
else if (stance == PlayerState.Crouch)
stance = PlayerState.Stand;
}
// Toggle prone
if (playerInputHandler.ProneTriggered)
{
if (stance == PlayerState.Stand || stance == PlayerState.Crouch)
stance = PlayerState.Prone;
}
// Set height based on stance
switch (stance)
{
case PlayerState.Stand:
currentHeight = standingHeight;
break;
case PlayerState.Crouch:
currentHeight = crouchHeight;
break;
case PlayerState.Prone:
currentHeight = proneHeight;
break;
}
characterController.height = Mathf.Lerp(characterController.height, currentHeight, Time.deltaTime * crouchTransitionSpeed);
}
This is what I have now and it made the issues worse
The player object is half stuck in the ground when testing and crouching or going prone makes him sink through the floor. Also the input for crouching is really inconsistent, like sometimes Ill press it and it crouches but then Ill press it again to stand up and it works but sometimes it also has the chance to go prone instead.
Also going from prone -> crouch pressing the crouch trigger just doesn't work either
where did you read you only change controller height?
of course it goes in the ground
also ofc the rest doesn't make sense thats why its broken
Sorry im not sure what you are referring to
characterController.height =
did you write any of this code?
I wrote some of it and it didn't work so I plugged it into AI and it gave me this
The switch stance stuff was what it added
Figured