Hi, Ive made a crouch script and when i do, it works great… BUT, when I look up or down and then crouch i fall through the world. It uses the standard Unity FPS controller, ive just added the crouch script to it…
Heres the script:
public bool isCrouching;
public float crouchHeight;
public float standardHeight;
public CharacterController controller;
public GameObject mainCam;
public GameObject gameObj;
public Vector3 startPos;
public float length;
public float camTest;
public float crouchCamHeight;
public float standardCamHeight;
public float crouchDrop= -0.25f;
void Start () {
controller = GetComponent<CharacterController>();
mainCam = GameObject.FindWithTag("CamB");
gameObj = GameObject.Find("Robot_B(Clone)");
standardHeight = 1.7f;
crouchHeight = 1.1f;
standardCamHeight = 1.0f;
crouchCamHeight = -0.3f;
}
void Update () {
if (Input.GetButtonDown("Crouch")) {
if (isCrouching) {
UnCrouch ();
}
else {
Crouch ();
}
}
}
void UnCrouch () {
if (networkView.isMine) {
controller = GetComponent<CharacterController>();
mainCam.transform.localPosition = new Vector3(0,standardCamHeight, 0.5f);
controller.center = new Vector3(0,0,0);
controller.height = standardHeight;
GameObject RobBCrouch = GameObject.Find("ROBB");
ROBOT_B_ANIMATE crouchComp = RobBCrouch.GetComponent<ROBOT_B_ANIMATE>();
crouchComp.UnCrouch();
isCrouching = false;
}
}
void Crouch () {
if (networkView.isMine) {
controller = GetComponent<CharacterController>();
mainCam.transform.localPosition = new Vector3(0,crouchCamHeight, 0.5f);
controller.center = new Vector3 ( 0, crouchDrop, 0);
controller.height = crouchHeight;
GameObject RobBCrouch = GameObject.Find("ROBB");
ROBOT_B_ANIMATE crouchComp = RobBCrouch.GetComponent<ROBOT_B_ANIMATE>();
crouchComp.Crouch();
isCrouching = true;
}
}
ignore the networking code.