FPS Crouching

Hi, im new to scripting and i was wondering if someone could help me. I created an fps game based off of the free tutorial and i was wondering could i make my character crouch ? Any help will be appreciated ! :wink:

I don’t remember i that game used a player model or only the arm! If only the arm, I would make it so when the player presses a button like c, the camera and arm are moved down however much. This would simulate a crouch.

You could also shorten the ā€œcharacter Controllerā€, assuming you have one… you can just say playerController.height = X, and playerController.center = XVector. This way, the collision boundaries of the player will be altered as well.

The only thing is that i dont know how to write the full code! :lol:

Something like:

function Update() {
if (Input.GetKey("c")) {
playerController.height = 0.5;
playerController.center = 0.5Vecror
}
}

THis may be wrong, however, and is certainly incomplete, but it sould give you a place to start! :smile: :smile: :smile:

Ok, I am trying to do this now, but the console says CharacterController is not static, so it cant be changed. Any idea how to do this? :?

Thank you SOOOOO much !!! :smile:

Actually, it doesnt work, I havent figured out how to make the CharacterController static, but I found a good alternative:

	if (Input.GetKey ("c")) {
		transform.localScale.y = 0.5;
	} else { 
		transform.localScale.y = 1;
	}

It modifys the scale of the object, and works pretty well. But IDK if it changes the childs scale too, so experiment! :smile:

Anyone have an Idea of how to do this? :?

To get variables from the character controller do:

var controller : CharacterController = GetComponent(CharacterController);
controller.height = .5;

Check the Wiki page. I know there are multiple FPS scripts there.

the script suggested scales the child as well as the controller. does anyone know a way of getting around this?

I think anyone interested in FPS should check out Dastardly Banana’s weapon package. It also contains a player controller that has crouch.

http://forum.unity3d.com/viewtopic.php?t=47742

How about this:

function Update () {
if () {
input GetButtonDown("c");
transform.translate((Camera)Y*0.5);
}
}

Not sure if it will work b/c i’m only 14 and i’m at school just on the forums and can’t do a test on the script. If it does work, though, tell me.

I have it:

function Update() {
var controller = GetComponent(CHaracterController);
if (Input.GetButton("Crouch") {
controller.height = 0.5;
} else {
controller.height = 1;
}
}

Works

function Update() {
var controller = GetComponent(CharacterController);
if (Input.GetButton(ā€œCrouchā€)) {
controller.height = 1;
}
else {
controller.height = 2;
}
}

yes that works but when you stand back up you go through the floor.

Ok I have an idea from a none scripter point of view

What if instead of using a capsule collider, or cube and changing the height of it. You have two spheres?

O
O

Like that, one above the other. Then when you crouch, you turn the top one off, along with the crouch animation. Stand up again and turn it back on. wouldn’t that work and achieve the whole crouching thing, without having the player fall through floors etc?

that would work… but there is a better way via script, but again that would work.