- Home /
how to make an fps character crouch
ive been working on a fps game for the past few days but I don't know how to make my character crouch please can you help me. by the way I'm quite new to unity so please don't make the answer to complicated.
Okay, Have you made your character? Or are you still using the default capsule as the graphic?
i am currently using the default capsule because i dont know how to make a character.
Answer by skovacs1 · Nov 22, 2010 at 08:08 PM
There's a whole bunch to consider as far as what else your character is doing (can you jump when crouched? Is it different than jumping otherwise? How is it different? Does your movement speed change when crouching? etc.). You don't include any information about your setup such as your camera setup, your character setup, whether you are using a CharacterController, do you have animations, etc. The simple approach (while making some broad assumptions about what you are doing because you don't describe at all) is something like:
var crouching : boolean = false; var altHeightChange : float = 3.0f; private var controller : CharacterController;
function Start() { controller = GetComponent(CharacterController); }
function Update() { if(controller && Input.GetButtonUp("Crouch")) { //Crouch on/off crouching = !crouching; if(crouching) { controller.center.y -= altHeightChange 0.5f; controller.height -= altHeightChange; //move your camera down by altHeightChange animation.CrossFade("crouch"); } else { controller.center.y += altHeightChange 0.5f; controller.height += altHeightChange //move your camera up by altHeightChange animation.CrossFade("stand"); } } }
If you wanted crouch only when the key is held down, something like this:
var altHeightChange : float = 3.0f; private var controller : CharacterController;
function Start() { controller = GetComponent(CharacterController); }
function Update() { if(controller) { if(Input.GetButtonDown("Crouch")) { //Crouch on controller.center.y -= altHeightChange 0.5f; controller.height -= altHeightChange; //move your camera down by altHeightChange animation.CrossFade("crouch"); } if(Input.GetButtonUp("Crouch")) { //Crouch off controller.center.y += altHeightChange 0.5f; controller.height += altHeightChange //move your camera up by altHeightChange animation.CrossFade("stand"); } } }
If you have more states than crouching or standing, such as prone or jumping, etc. you might consider using and enum for your state.
brill script but, do you know how i can find how to make the camera down 2... thanks :D great script though
I officially love you for this script kind sir, if you happen to be in Düsseldorf, drinks on me!
Answer by oliver-jones · Nov 23, 2010 at 04:16 AM
You might want to take a look at this post too:
http://answers.unity3d.com/questions/14803/how-to-make-the-fps-character-crouch
Your answer

Follow this Question
Related Questions
How to Make the FPS Character Controller RUN and CROUCH 0 Answers
How To Make The FPS Character Crouch 8 Answers
force player to crouch when under an object with collisio 2 Answers
FPS Character controller crouch sneak 0 Answers
Make character either crouch or go prone depending on how long a button is held? 0 Answers