x


How To Make The FPS Character Crouch

I have put together a simple FPS game, but I just can't get my character to crouch. I've searched it up and have seen people shorten the Character Controller, but what I would like is to have the character controller shorten, and the camera as well. This way, the character can squeeze into places like vents, holes, or whatever. I've seen Controller.height = 0.5, but I also would like the camera to move down with it.

Thanks in advance :D

more ▼

asked Jul 08 '10 at 06:26 PM

Flipbee9 gravatar image

Flipbee9
73 19 20 25

(comments are locked)
10|3000 characters needed characters left

7 answers: sort voted first

This is what I do, the function crouch lowers the character controller height & collider and sets crouching to true. crouchDeltaHeight is my variable for the height change when crouching. (All of this would be in the FPSWalker default script).

    function crouch() {
        this.GetComponent(BoxCollider).size -= Vector3(0,crouchDeltaHeight, 0);
        this.GetComponent(BoxCollider).center -= Vector3(0,crouchDeltaHeight/2, 0);
        crouching = true;
    }

And then in update the camera lowers to crouch height if crouching or returns to normal otherwise (it's in update to make it look smooth).

if(crouching){
    if(mainCamera.transform.localPosition.y > crouchingCamHeight){
        if(mainCamera.transform.localPosition.y - (crouchDeltaHeight*Time.deltaTime*8) < crouchingCamHeight){
            mainCamera.transform.localPosition.y = crouchingCamHeight;
        } else {
            mainCamera.transform.localPosition.y -= crouchDeltaHeight*Time.deltaTime*8;
        }
    }
} else {
    if(mainCamera.transform.localPosition.y < standardCamHeight){
        if(mainCamera.transform.localPosition.y + (crouchDeltaHeight*Time.deltaTime*8) > standardCamHeight){
            mainCamera.transform.localPosition.y = standardCamHeight;
        } else {
            mainCamera.transform.localPosition.y += crouchDeltaHeight*Time.deltaTime*8;
        }
    }

}

This is the code to stop crouching and return to normal:

        function stopCrouching(){
            crouching = false;
            this.GetComponent(BoxCollider).size += Vector3(0,crouchDeltaHeight, 0);
            this.GetComponent(BoxCollider).center += Vector3(0,crouchDeltaHeight/2, 0);
        }

And here's what I have in update to trigger it:

    if (Input.GetButtonDown ("Crouch")){
    if(crouching){
        stopCrouching();
        return;
    }

    if(!crouching)
        crouch();
}
more ▼

answered Jul 08 '10 at 06:42 PM

Jason_DB gravatar image

Jason_DB
1.9k 4 14 36

Again, another brilliant Answer from DastardlyBanana :D Thanks very much

Jul 08 '10 at 06:43 PM Flipbee9

I keep getting errors about crouchDeltaHeight, standardCamHeight and etc. I'm guessing I'm supposed to set up the variables and so I did so. But about the BoxCollider, I'm using a character controller instead of a box collider

Jul 08 '10 at 07:15 PM Flipbee9

Well then you can probably just cut that out.

Jul 08 '10 at 07:48 PM Jason_DB

how do i make this work

Aug 12 '10 at 08:51 PM josh
(comments are locked)
10|3000 characters needed characters left

so how do i put in that code? sorry i just started unity yesterday and i have a good fps going now

more ▼

answered Aug 07 '10 at 07:28 PM

josh gravatar image

josh
2 2

(comments are locked)
10|3000 characters needed characters left

My suggestion would be having five variables, for the hieght and camera position before and after crouching and a variable that ays if you are crouching. Have the camera position be relative, so that it is always added to the characters own position. If your weapons are a child of your camera then you will see them move down as well. This may be an issue with graphics, as if you are using a mesh for the character you will have to create an entire motion sequence seperate from the original and have them programmed individualy (Animation Programing can be a pain in FPS) and afterwards should you want a death scene the rigidbody formed would have to be altered for a 'crouch death'. Lastly you would be forced to decide how your crouching affects your jumping, whether you can jump when crouched, if so can you jump as high, and that would involve delving into the fps character made by unity (I am presuming you are using it.) which is a difficult process.

more ▼

answered Apr 12 '11 at 05:05 PM

Donald Seburn gravatar image

Donald Seburn
14 4 4 5

(comments are locked)
10|3000 characters needed characters left

Fore anyone trying to use the FPS controller:

private var walkSpeed: float = 8;    // regular speed

private var runSpeed: float = 15;    // run speed

private var crouchSpeed: float = 3;  // crouching speed

private var speed: float;            // the current speed state



private var crouchHeight : float;     // variables for character height while crouching

private var crouchingHeight : float;  

private var standingHeight: float;    // height of character while standing

private var crouching = false;        // boolean is crouching?

private var running = false;          // boolean is running?



private var characterMotor: CharacterMotor;  //direct variable to the characterMotor

private var mainCamera: GameObject;          // direct variable to the mainCamera



function Start(){

    //initialize

    characterMotor = GetComponent(CharacterMotor);

    mainCamera = gameObject.FindWithTag ("MainCamera");

    standingHeight = mainCamera.transform.localPosition.y;

    crouchingHeight = (standingHeight / 2);

    crouchHeight = (standingHeight - crouchingHeight);

}



function Update(){



    //simple machine for setting the current speed of the character

    if( crouching ){

        speed = crouchSpeed;

    }else if( running ){

        speed = runSpeed;

    }else{

        speed = walkSpeed;

    }



    //If the sprint button is clicked, and not crouching .. SPRINT!

    if (Input.GetKey("left shift") || Input.GetKey("right shift")){

        if(!crouching){

            running = true;

            CameraZoom.sprint = true;  //sets sprint true so the camera knows to pan out

        }

    }else {

        CameraZoom.sprint = false;

        running = false;

    }



    // if crouch button has been pressed, put FPS into crouch

    if (Input.GetButtonDown ("Crouch")){

        if(crouching){

            stopCrouching();

            return;

        }

        if(!crouching)

             crouch();

    }



    // crouch with smoothing

    if(crouching){

        if(mainCamera.transform.localPosition.y > crouchingHeight){

            if(mainCamera.transform.localPosition.y - (crouchHeight*Time.deltaTime*8) < crouchingHeight){

                mainCamera.transform.localPosition.y = crouchingHeight;

            } else {

                mainCamera.transform.localPosition.y -= crouchHeight*Time.deltaTime*8;

            }

        }

       } else {

        if(mainCamera.transform.localPosition.y < standingHeight){

            if(mainCamera.transform.localPosition.y + (crouchHeight*Time.deltaTime*8) > standingHeight){

                mainCamera.transform.localPosition.y = standingHeight;

            } else {

                mainCamera.transform.localPosition.y += crouchHeight*Time.deltaTime*8;

            }

            }

    }

    //set the max speed in the char motor;

     characterMotor.movement.maxForwardSpeed = speed;

}



// sets teh size of the collider to crouch size

function crouch() {

        this.GetComponent(CharacterController).height -= crouchHeight;

        this.GetComponent(CharacterController).center -= Vector3(0,crouchHeight/2, 0);

        crouching = true;

 }

// resets the size of the collider to standing size  

 function stopCrouching(){

        crouching = false;

        this.GetComponent(CharacterController).height += crouchHeight;

        this.GetComponent(CharacterController).center += Vector3(0,crouchHeight/2, 0);

}  
more ▼

answered Oct 01 '11 at 09:10 PM

fAvorable gravatar image

fAvorable
1

(comments are locked)
10|3000 characters needed characters left

I don't know if this is the best solution but I was thinking as long as you had the camera parented to the capsule you are using as your character you could just make an animation that scales the capsule on the y-axis.

more ▼

answered Aug 20 '10 at 06:46 PM

Kevin Ambruster gravatar image

Kevin Ambruster
28 1 1 3

you could do that but I've learned through sad experience that when you do that an you're holding a gun or other mesh it gets scrunched too.

Oct 06 '10 at 06:36 PM Jordan Miller 2

however that can be remedied by replacing the mesh every time you crouch or stand up.

Oct 06 '10 at 06:37 PM Jordan Miller 2
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1169
x1039
x40

asked: Jul 08 '10 at 06:26 PM

Seen: 6107 times

Last Updated: Mar 05 '12 at 02:59 AM