- Home /
Crouching Script
Can anyone explain to me the code in the update function as I am having a hard time making sense of it.
#pragma strict
var walkSpeed : float = 7;
var crouchSpeed : float = 3;
private var charMotor : CharacterMotor;
private var charController: CharacterController;
private var theTransform : Transform;
private var charHeight: float;
function Start ()
{
charMotor = GetComponent(CharacterMotor);
theTransform = transform;
charController = GetComponent(CharacterController);
charHeight = charController.height;
}
function Update ()
{
var h = charHeight;
var speed = walkSpeed;
if (Input.GetKey("c"))
{
h = charHeight *0.5;
speed = crouchSpeed;
}
charMotor.movement.maxForwardSpeed = speed;
var lastHeight = charController.height;
charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);
theTransform.position.y += (charController.height - lastHeight)/2;
}
Answer by AlucardJay · May 21, 2014 at 11:47 AM
line 33 lerps the character controller height from its current height to h (h is either standing height or crouched height).
line 34 repositions the character so it doesn't fall through the floor when scaling to a larger height, and doesn't appear floating when scaling to a smaller height.
Edit :
Try and imagine it with real values.
From Standing to Crouching :
line 32 : lastHeight is 1
line 33 : imagine this set charController.height to 0.5
line 34 : (charController.height - lastHeight)/2 = (0.5 - 1) / 2 = -0.25
theTransform.position.y += -0.25 , so it lowers the position Y so that the bottom of the collider is still on the ground
From Crouching to Standing :
line 32 : lastHeight is 0.5
line 33 : imagine this set charController.height to 1
line 34 : (charController.height - lastHeight)/2 = (1 - 0.5) / 2 = 0.25
theTransform.position.y += 0.25 , so it raises the position Y so that the bottom of the collider is still on the ground
Thank you for the explanation. I understand how lerp works now, but i'm going to look a bit further into the last line as it seems somewhat complex.
so when the collider sinks into the ground the position of it is subtracted so that the part thats intersecting is the new bottom?
Here is a script to demonstrate the process. In a new scene, create an empty gameObject, and attach this script. Hit play, and move the slider, you'll see the bottom of the collider is always in the same place :
#pragma strict
private var myCapsule : Transform;
private var myCollider : CapsuleCollider;
private var charHeight: float;
private var modifier : float = 1;
private var graphic : Transform;
function Start()
{
var go : GameObject = GameObject.CreatePrimitive( PrimitiveType.Capsule );
go.name = "Capsule Collider";
myCapsule = go.transform;
myCapsule.renderer.enabled = false;
myCollider = myCapsule.GetComponent( CapsuleCollider );
charHeight = myCollider.height;
// graphic
go = GameObject.CreatePrimitive( PrimitiveType.Capsule );
go.name = "Capsule Graphic";
go.GetComponent( CapsuleCollider ).enabled = false;
graphic = go.transform;
}
function Update()
{
var h : float = charHeight * modifier;
var lastHeight : float = myCollider.height;
myCollider.height = $$anonymous$$athf.Lerp( myCollider.height, h, 5 * Time.deltaTime );
myCapsule.position.y += ( myCollider.height - lastHeight ) / 2;
// graphic
graphic.position.y = myCapsule.position.y;
graphic.localScale.y = myCollider.height * 0.5;
}
function OnGUI()
{
modifier = GUI.HorizontalSlider( Rect( 20, 20, 200, 25 ), modifier, 0.5, 1.0 );
}
ah ok I see what your saying. the math adds up. I was getting confused because it looks like the capsule is sinking into the ground. The graphics threw me off. Thank you very much for the explanation. I understand what its doing now.
I understand, you can see in my test script I compensated for this.
The best way to watch the collider is in the hierarchy, click on the character controller, in the scene view just watch the green wireframe of the collider.
Your answer
