Help! Need to fix this script.

Hi,i have made simple sprint script,but when i run and try to jump,its getting realy buggy sometimes,well jumping is buggy when that script is attached.And the problem is “gravity” without gravity it could work perfectly,BUT I cannot delete gravity from the script,because its main part that i can even sprint,so guys i realy need your help,can someone please modify this script that it work perfect (i can jump without glitches and bugs) TRY YOURSELF,and please help me! :slight_smile:

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var ETCSpeed = 6.0;
var SSpeed = 8.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
 
function Update(){
if(Input.GetButtonDown("Sprint")){
Sprint();
}else{
if(Input.GetButtonUp("Sprint")){
StopSprint();
}
}
}
function FixedUpdate() {
        if (grounded) {
                // We are grounded, so recalculate movedirection directly from axes
                moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
                moveDirection = transform.TransformDirection(moveDirection);
                moveDirection *= speed;
               
                if (Input.GetButton ("Jump")) {
                        moveDirection.y = jumpSpeed;
                }
        }
 
        // Apply gravity
        moveDirection.y -= gravity * Time.deltaTime;
       
        // Move the controller
        var controller : CharacterController = GetComponent(CharacterController);
        var flags = controller.Move(moveDirection * Time.deltaTime);
        grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}
function Sprint(){
speed = SSpeed;
audio.Play();
}
function StopSprint(){
speed = ETCSpeed;
audio.Stop();
}
 
@script RequireComponent(CharacterController)

A few tinkers to it, adjusting for picking up speed and not setting it. Also a test to see what happens when we “lerp” the new speed to teh old one. Should result in a gradual speed up, and not a forced speed. u can always multiply Time.fixedDeltaTime by something if things dont move fast enough.

	var speed = 6.0;
	var jumpSpeed = 8.0;
	var gravity = 20.0;
	var ETCSpeed = 6.0;
	var SSpeed = 8.0;
	private var moveDirection = Vector3.zero;
	private var grounded : boolean = false;
	 
	function Update(){
		// update logic
		
		if(Input.GetButtonDown("Sprint")){
			Sprint();
		}else{
			if(Input.GetButtonUp("Sprint")){
				StopSprint();
			}
		}
	}
	function FixedUpdate() {
			// assign teh controller first
			var controller : CharacterController = GetComponent(CharacterController);
			
			// pick up moveDirection from the character controller.
			// So u can stop at walls and not continue force past them
			moveDirection = controller.velocity;
			
			// limit that movement so that u dont jump at full speed
			// do x and z only, y should remain gravity.
			moveDirection.x = Mathf.MoveTowards(moveDirection.x, 0, Time.fixedDeltaTime);
			moveDirection.z = Mathf.MoveTowards(moveDirection.z, 0, Time.fixedDeltaTime);
			
			if (grounded) {
					// We are grounded, so recalculate movedirection directly from axes
					//moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
					
					// as a test, try Lerping the movement towards the new move, not forcing it.
					var newMove:Vector3 = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
					moveDirection = Vector3.MoveTowards(moveDirection, newMove, Time.fixedDeltaTime);
					
					moveDirection = transform.TransformDirection(moveDirection);
					moveDirection *= speed;
					
					// make sure that the y stops falling at high speed once we hit the ground.
					moveDirection.y = gravity * Time.fixedDeltaTime;
				   
					// GetButtonDown, not GetButton
					if (Input.GetButtonDown ("Jump")) {
						moveDirection.y = jumpSpeed;
					}
			}
	 
			// Apply gravity
			moveDirection.y -= gravity * Time.fixedDeltaTime;
		   
			// Move the controller
			var flags = controller.Move(moveDirection * Time.fixedDeltaTime);
			grounded = (flags  CollisionFlags.CollidedBelow) != 0;
	}
	function Sprint(){
		speed = SSpeed;
		audio.Play();
	}
	function StopSprint(){
		speed = ETCSpeed;
		audio.Stop();
	}
	 
	@script RequireComponent(CharacterController)