i am making a unity crouching script much like the minecraft one
i want the character to stay crouched when under obstacles and to not fall off edges(like in minecraft)
i have no idea how and nothing i tried works
i have this so far (but i fall through the floor when i uncrouch)
if (Input.GetKeyDown(crouch)) {
cc.height -= CrouchCamLower;
}
if (Input.GetKeyUp (crouch))
{
cc.height += CrouchCamLower;
}
I am assuming the crouchCamLower variable holds a value that you are down scaling the character controller by to get the “illusion” of crouching. When the Character controller rescales it’s height once the player uncrouches, try ALSO raising the position.y of the character controller to bring it up at the same time. Remember, we fall through the floor when our collider(In this case the character controller) is clipping through the plane(or terrain) collider.
What is the ground? A plane?
Also, try this:
if (Input.GetKeyDown(crouch)) {
cc.height -= CrouchCamLower;
}
if (Input.GetKeyUp (crouch))
{
RaycastHit ray;
cc.height += CrouchCamLower;
if (Physics.Raycast(transform.position,transform.TransformDirection(Vector3.down),ray)
{
}
else {
transform.position = new Vector3(transform.position.x, Ground.transform.position.y + 1,transform.position.z);
}
}
You will need to make a variable that is the ground called Ground.
If it doesn’t work then reply to this answer and I’ll write another script.