Crouching for the FPS controller

As stated in another thread I’m trying to comprehend coding. Since I was told the best way is learning by doing here’s my problem: I’m trying to expand the FPS Walker script to some more functions. My first try was adding a basic crouch function.

So what I did was expand the standard script. The problem is that decreasing the size of the player works perfet but getting up after relasing the button works apparently only random.
What am I doing wrong? Playing around with the various GetButton states didn’t work too well, either. The result’s always the same.

private var StandingSize = 1.0;

function Start() {
	StandingSize = GetComponent(CharacterController).height;
}

// had this in one function but tried splitting it up, now the result's the same...
if (Input.GetButton("Crouch")) {
			Crouch();
		} else if (Input.GetButtonUp ("Crouch")) {
			GetUp();
		}

function Crouch() {
	var controller : CharacterController = GetComponent(CharacterController);
	print("StandingSize Variable is now " + StandingSize); // debug message
	controller.height = crouchSize;
}

function GetUp() {
	var controller : CharacterController = GetComponent(CharacterController);
	print("Character Trying to get up to Size: " + StandingSize); // debug message
	controller.height = StandingSize;
}

I’ve left out all the other code that remains in the original script since I didn’t change it.

In case it’s necessary I inluded the whole script, too → see attachment.

141205–5161–$fpswalker_2_988.js (1.47 KB)

Hey motionblur, if you haven’t fixed this problem yet, I believe I have a solution. I am new here also and attempting to learn the scripting language/API and how everything works.

My problem was that after you crouch, when you try to stand back up, you fall underground. I just figured out what was happening and how to fix it.

When you adjust the CharacterController’s height, it expands the collision capsule as well (that is why you get closer to the ground when your height gets smaller). If you add 3 to the height, it will reach 1.5 units higher, and 1.5 units lower. This is fine as long as you are just reducing the size of the CharacterController. But when you raise it, part of the capsule goes underneath the terrain (assuming that is what you are standing on), so the whole object falls through.

The solution to this, of course, would just to be to compensate for the amount that the capsule has grown lower by raising it along the y-axis that high.

So essentially, if your CharacterContoller’s height was originally 6, but you made it 3 when it was “crouched”, when it goes to standup, it would need to move up 1.5 units to compensate for the 1.5 units that it has extended down.

My code looks like this:

var isCrouched : boolean = false;
function Update () {
	if(Input.GetKey("c")) //is the crouch button being held?
	{
		if(!isCrouched)
			crouch();
		
	}
	else
	{
	   if(isCrouched)
	      stand();
	}
}

function crouch()
{
	var controller : CharacterController = GetComponent(CharacterController);
	controller.height /= 2;
	isCrouched = true;
}

function stand()
{
	var controller : CharacterController = GetComponent(CharacterController);
	
       //the "fpv" was a tag added to First Person Controller so it can be modified
       var fpvController = gameObject.FindWithTag("fpv");
	fpvController.transform.position.y += controller.height/2;
	controller.height *= 2;

 	isCrouched = false;
}

And it works fine. This may not be the most efficient way of moving the controller, but it works for me.

ah - thanks for the reply. I figured it out myself, now. The problem I had was that I had the GetButtonUp statement in the FixedUpdate() - which only ocasionally worked. Writing it inside the Update() function works flawlessly.

Now all I need to figure out is how to translate the capsule upwards without the snapping but rather in a smooth transition :slight_smile: