- Home /
Increased the variable height
Strange falling through world when crouching
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.
Indeed as suggested below, it seems you are pushing your Collider through the floor.
Answer by tone20 · May 16, 2014 at 12:32 PM
the problem is when your first person controller crouches it actualy goes half in the ground and if you set croush limit to very hight your whole player will crouch under world
its not though, the camera has nothing to do with the players controller height or positioning, and yet when i look up (and crouch) i fall through but if i look forward and crouch its fine....
Answer by gdubrocks · May 11, 2014 at 06:42 AM
My guess is that the first person camera controller uses the current camera location to decide the player location, so when you lower the camera by crouching your player is lowered as well and falls through the ground.
Maybe you could try thickening the ground beneath you to stop yourself from falling though, or force the FPS controller to always use the main camera.
You also seem to be changing the position of the controller. Try instead changing the camera position.
If you make crouchDrop 0 does your char still fall through the ground? If so it might be an animation issue.
no setting it to 0 didnt help..
It seems if i disable the mouselook on the whole player object (it turns the player on the x axis so the controls are always facing the right way.. (theres one on the camaera that does the Y)) you stop falling through... but i need it enabled so that i can rotate on X
i tried adding a mesh collider to the ground, it still didnt fix it
It might help you to test what obviously ISN'T the problem and to remove that from your question. If it's not the crouchDrop then remove it, if it's not collision then remove it. That would make it easier to debug your code.
but I suspect it is one of these two lines.
controller.center = new Vector3 ( 0, crouchDrop, 0);
controller.height = crouchHeight;
controller.center = new Vector3 ( 0, crouchDrop, 0);
controller.height = crouchHeight;
controller.center just sets the center of the character controller back to the initial position, and height just sets it to the right size..
Answer by Cherno · May 17, 2014 at 12:59 PM
Take a look at this thread which deals with the issue you have.
Follow this Question
Related Questions
Horror Crouch Script Help 1 Answer
Keep Player Crouching 2 Answers
How to stop crouching automaticaly after getting out of a small area 0 Answers
Crouch Script with transform.localScale 0 Answers