I’m just speaking from a code point of view, I get that there will have to be accompanying animations to go with it, but from a code stand point what is the best way to achieve this? Unless the community here thinks there is a better way to achieve crouch, I was just going to shrink the capsule collider to half it’s height. I totally appreciate your feedback and tips. Thanks!
Well here is what I came up with and I’m pretty happy with it.
/* *
* Crouch
* */
if(Input.GetButton("Crouch"))
{
//Only permit a char to crouch if isn't already ducking
if (!isCrouching)
{
isCrouching = true;
//transform.localScale requires a Vector3, so we feed in the original x and z localscale, but we multiply the y localscale by .5 to halve it.
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y * .5f , transform.localScale.z);
//Debug.Log("Crouching.");
}
else
{
//Debug.Log("Can't Crouching. Already Crouching.");
}
}
//If we need to uncrouch
else if (Input.GetButtonUp("Crouch"))
{
//Only permit uncrouching if the char isn't under something that inhibits this.
if (!Physics.Raycast(transform.position, Vector3.up, 1f))
{
isCrouching = false;
//transform.localScale requires a Vector3, so we feed in the original x and z localscale, but we devide the y localscale by .5 to double it (back to it's original height).
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y / .5f , transform.localScale.z);
Debug.Log("Crouch released, no longer crouching.");
}
else
{
crouchLock = true;
Debug.Log("CrouchLocked. Cannot be released due to being under an object.");
}
}
When the Crouch button is pressed, the character crouches (IE. The transform.localScale.y is actually halved). When the crouch button is released if the player is able to stand up (A raycast to check to see if there is anything physically above the transform) the transform.localScale.y is returned to it’s original value. So if the player is crouching under an object and it is not allowed to stand up but the player releases the crouch button anyways, then a variable called crouchLocked is set to true.
Then there is a function called releaseCrouchLock() that runs in update(). This function checks to see if the player uncrouch, again with the raycast.
//Release the character from crouching when appropriate to do so (when the char is physically able to stand up with out hitting something)
void releaseCrouchLock()
{
if(crouchLock)
{
//Only permit the char to uncrouch if the character isn't under anything
if (!Physics.Raycast(transform.position, Vector3.up, 1f) isCrouching)
{
//transform.localScale requires a Vector3, so we feed in the original x and z localscale, but we devide the y localscale by .5 to double it (back to it's original height).
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y / .5f , transform.localScale.z);
//Disable crouching
isCrouching = false;
//Disable Crouchlock
crouchLock = false;
Debug.Log("Crouch Lock released, no longer crouching.");
}
}
}