hi can someone explains this script :

im not that person who copy other script without learn it , so i have the crouch script , but i want to know how it works ( sorry im not ewnglish xD) :

var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
 
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float; // initial height
 
function Start(){
    chMotor = GetComponent(CharacterMotor);
    tr = transform;
    ch = GetComponent(CharacterController);
	height = ch.height;
}
 
function Update(){
 
    var h = height;
    var speed = walkSpeed;
    
    if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
        speed = runSpeed;
    }
    if (Input.GetKey("c")){ // press C to crouch
        h = 0.5 * height;
        speed = crchSpeed; // slow down when crouching
    }
    chMotor.movement.maxForwardSpeed = speed; // set max speed
    var lastHeight = ch.height; // crouch/stand up smoothly 
    ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
    tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
}

for first thing i would learn why aldo has remakes the height variable on update function , but i wanna know all the script :slight_smile:

thanks in advance :smiley:

var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed

these are the different set speed, walk, crouch and run. While the script is running they are constant, but they can be changed manually in the inspector.

private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float; // initial height
 
function Start(){
    chMotor = GetComponent(CharacterMotor);
    tr = transform;
    ch = GetComponent(CharacterController);
    height = ch.height;
}

The characters variables and the initial start up function that sets the variables up with the other components attached to the GameObject.

function Update(){
 
    var h = height;
    var speed = walkSpeed;

The update function is called once per frame.
h and speed are the variables that are going to be used to as the final desired height and speed of the controller and motor.

    if (ch.isGrounded && Input.GetKey("left shift") ||       Input.GetKey("right shift")){
        speed = runSpeed;
    }

This if statement asks if the controller is on the ground (so you cannot crouch mid air) and if you are holding either left or right shift down.

|| means or

if its true then the speed equals run speed.

    if (Input.GetKey("c")){ // press C to crouch
        h = 0.5 * height;
        speed = crchSpeed; // slow down when crouching
    }

This if statement is saying, if you are pressing down ā€˜c’ then the desired height (h) equals half the original height and the desired speed equals the crouch speed.

    chMotor.movement.maxForwardSpeed = speed; // set max speed
    var lastHeight = ch.height; // crouch/stand up smoothly 

is exactly what the comments say.

ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);

information on Lerp can be found here: Unity - Scripting API: Mathf.Lerp
but the simple version is that it smoothly transitions from one float to another with a speed. so instead of 3 going to 6 it would go 3, 4, 5, 6.

tr.position.y += (ch.height-lastHeight)/2; // fix vertical position

This stops the character from being under the floor, it puts the character half the height of its original height higher.