- Home /
FPS movements.
Hi, i need some help! I searched in this site a script to add some movements (crouch and run) on FPS controller: here is the script
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("left ctrl")){ // press Ctrl 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
}
If I press LeftShift to run and then I release it, the character slides on the floor too, but I want avoid this.
How can I adjust this script?
P.S: Sorry for my bad english and my ignorance.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613165745im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI Slider and Movement? 2 Answers
Buggy Collision 1 Answer
camera slides and bounces while moving C# 1 Answer
Help with FPS slide mechanic 0 Answers
How to make a cube slide and can't change direction? 2 Answers