Modifying the Character Motor Script

hi all

I want to customize the "CharacterMotor" script found in the FPS controller prefab of unity3.

I want to add a script command, say when left shift is pushed then movement speed is increased.

I have tried various ways and none have worked and i also have basic knowledge of scripting.

Much help would be appreciaed

Yup me to this script is so god dam unexplainable itself so much but yet so nothing...

1 Answer

1

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

private var chMotor: CharacterMotor;
private var tr: Transform;
private var dist: float; // distance to ground
 
function Start(){
    chMotor = GetComponent(CharacterMotor);
    tr = transform;
    var ch:CharacterController = GetComponent(CharacterController);
    dist = ch.height/2; // calculate distance to ground
}
 
function Update(){
 
    var vScale = 1.0;
    var speed = walkSpeed;
 
    if (chMotor.grounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
        speed = runSpeed;
    }
    if (Input.GetKey("c")){ // press C to crouch
        vScale = 0.5;
        speed = crchSpeed; // slow down when crouching
    }
    chMotor.movement.maxForwardSpeed = speed; // set max speed
    var ultScale = tr.localScale.y; // crouch/stand up smoothly 
    tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
    tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
}

for some reason the website has put it all beside each other but just press enter after each of the semicolins to line it up and add that to ur character controller and you will now have a run and a crouch key

To format code in a UA question, select the code and use the 101/010 button. I did your code for you.