- Home /
C# Problem returning from crouch to stand
I'm trying to make a simple crouch that when the crouch button is pressed the player crouches and stays crouched until the button is pressed again, where they return to standing. By just having my first if I can get my player to crouch with getButton but adding the second if to return them to crouching causes the crouch to become random and shaky. Does anyone have any ideas... Thanks
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class CrouchAndProne2 : MonoBehaviour
{
CharacterController characterController;
public bool isStanding = true;
public bool isCrouching = false;
float inHeight = 0.0f;
Vector3 inCenter;
public float standingHeight = 0.0f;
public Vector3 standingCenter;
public float crouchingHeight = 0.0f;
public Vector3 crouchingCenter;
public Transform mainCam;
public Vector3 defCamHeight;
public Vector3 curCamHeight;
public Vector3 crouchCamHeight;
float camSpeed = 20.0f;
// Use this for initialization
void Start ()
{
characterController = GetComponent<CharacterController>();
isStanding = true;
//isCrouching = false;
inHeight = characterController.height;
inCenter = characterController.center;
standingHeight = inHeight;
standingCenter = inCenter;
crouchingHeight = inHeight - 0.451f;
crouchingCenter = inCenter + Vector3.down * 0.2f;
}
void Awake()
{
defCamHeight = mainCam.localPosition;
}
// Update is called once per frame
void Update ()
{
//Make camera follow crouch and prone.
if(isStanding == true)
{
curCamHeight = defCamHeight;
}
if(isCrouching == true)
{
curCamHeight = crouchCamHeight;
}
//Lerp
mainCam.localPosition = Vector3.Lerp(mainCam.localPosition, new Vector3(0.0f, curCamHeight.y, 0.0f), camSpeed * Time.deltaTime);
//Make player go from standing to crouch.
if(Input.GetButton("Crouch") == true && isStanding == true)
{
Crouch();
}
else if(Input.GetButton("Crouch") == true && isCrouching == true)
{
Stand ();
}
}
void Stand ()
{
characterController.height = standingHeight;
characterController.center = standingCenter;
isStanding = true;
isCrouching = false;
}
void Crouch ()
{
characterController.height = crouchingHeight;
characterController.center = crouchingCenter;
isCrouching = true;
isStanding = false;
}
}
Answer by nesis · Mar 09, 2014 at 06:18 PM
if (Input.GetKeyDown(KeyCode.E)) {
if (isCrouching) {
Stand();
}
else {
Crouch();
}
}
Stand() and Crouch() should set isCrouching to false and true respectively, while also setting camera height, and the other things they do now.
Thanks for the reply :) I've tried the above but I'm still getting the same problem. Pressing crouch still only works with some presses and when it does work its kind of shaky. I have my Crouch button set to the b button of an xbox controller if that could be causing a problem... I've checked and my input settings look fine. Also GetButtonDown didn't do anything so I'm using GetButton. Thanks.
using Input.GetButton() will toggle it on and off rapidly, on one frame, off the next. You need to use Input.GetButtonDown() or Input.Get$$anonymous$$eyDown() to detect the frame the player started pressing crouch, rather than every frame the user is holding crouch.
Also, make sure you use that in the Update() or LateUpdate() methods, since button state is updated once per frame.