Read my latest comment for info…
This is the code I have the main section of problem is in the function Update
//Garth's FPS Controller
private var currentSpeed : float;
var walkSpeed : float = 5;
var runSpeed : float = 9;
var gravity : float = 20;
private var sprinting : boolean;
var airControl : boolean = false;
private var playerControl : boolean = false;
var jumpTimer : int;
var limitDiagonalSpeed = true;
private var moveDirection = Vector3.zero;
var jumpSpeed : float = 8;
private var inputX;
private var inputY;
private var inputModifyFactor;
private var myTransform : Transform;
private var falling : boolean = false;
private var fallStartLevel : float;
var fallingDamageThreshold : float = 10.0;
var grounded : boolean = false;
private var controller;
var sprintTimer : float = 0;
var playerName = "New Player";
var jumpLimit : int = 30;
var jumpHold : float = 30;
var jumping : boolean;
function Start () {
controller = GetComponent(CharacterController);
sprinting = false;
currentSpeed = walkSpeed;
myTransform = transform;
sprintTimer = Mathf.Clamp(10,0,10);
jumpTimer = jumpLimit;
jumping = false;
}
function FixedUpdate () {
inputY = Input.GetAxis("Vertical");
inputX = Input.GetAxis("Horizontal");
inputModifyFactor = (inputX != 0.0 && inputY != 0.0 && limitDiagonalSpeed)? .7071 : 1.0;
grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0;
if (!Input.GetButton("Jump")) {
jumpTimer++;
jumping = false;
}
else if (jumpTimer >= jumpLimit) {
jumping = true;
moveDirection.y = jumpSpeed;
jumpTimer = 0;
}
if (falling) {
falling = false;
if (myTransform.position.y < fallStartLevel - fallingDamageThreshold)
FallingDamageAlert (fallStartLevel - myTransform.position.y);
}
if (airControl && playerControl) {
moveDirection.x = inputX * currentSpeed * inputModifyFactor;
moveDirection.z = inputY * currentSpeed * inputModifyFactor;
moveDirection = myTransform.TransformDirection(moveDirection);
}
if (!falling) {
falling = true;
fallStartLevel = myTransform.position.y;
}
moveDirection.y -= gravity * Time.deltaTime;
if (sprinting == true) {
sprintTimer -= Time.deltaTime;
}
if (sprinting == false) {
sprintTimer += Time.deltaTime;
}
if (Input.GetButton ("Jump")) {
jumpHold -= 1 * Time.deltaTime;
}
if (jumpHold <= 0 && !jumping) {
jumpHold = 30;
jumpTimer = 30;
}
if (sprintTimer >=10) {
sprintTimer = 10;
}
if (sprintTimer <= 0) {
sprintTimer = 0; //Fix sprint timer..? add a cooldown or something like that
}
}
function Update () {
if (Input.GetAxis ("Vertical") > 0) { //Forward
transform.Translate(Vector3(0,inputY,currentSpeed) * Time.deltaTime);
}
if (Input.GetAxis ("Vertical") < 0) { //Backward
transform.Translate (Vector3(0,-inputY,currentSpeed) * Time.deltaTime);
}
if (Input.GetAxis ("Horizontal") > 0) { //Right
transform.Translate (Vector3 (0,0,0) * Time.deltaTime);
}
if (Input.GetAxis ("Horizontal") < 0) { //Left
transform.Translate (Vector3 (0,0,0) * Time.deltaTime);
}
if (Input.GetButton ("Run") && Input.GetButton ("Vertical") && sprintTimer >= 1 && grounded == true){
currentSpeed = runSpeed;
sprinting = true;
}
else {
currentSpeed = walkSpeed;
sprinting = false;
}
if (Input.GetButton ("Jump")) {
jumpHold -= 1 * Time.deltaTime;
}
if (jumpHold <= 0 && !jumping) {
jumpHold = 30;
jumpTimer = 30;
}
if (sprintTimer <= 0) {
currentSpeed = walkSpeed;
sprinting = false;
sprintTimer = 0;
}
//Printing Section
//print ("Speed = " + currentSpeed);
//print ("Players Name = " + playerName);
//print ("Sprinting = " + sprinting);
//print ("Jumping = " + jumping);
}
function FallingDamageAlert (fallDistance : float) {
print ("Ouch! Fell " + fallDistance + " units!");
//Add sounds effects! + Health System
}
@script RequireComponent(CharacterController)
Any suggestions on what I could change to make the script better would be appreciated aswell
(Code updated to match question in the latest comment i made)