Indent your code correctly. It's really hard to trace the execution with a mess like this.
What exactly is "not working"? Could you please be more specific about what you are expecting and what your code is doing? Are you getting any errors? If so, what are they?
There's a whole bunch missing from the code posted before it will do anything. There's no declaration of canRun, canSprint, sprintSpeed, speed, controller, sprintTime or outOfPower. There's no function that's being called that will execute this. It's unclear if you just omitted it because it was assumed to exist or if it is actually missing from the code.
Here's some code that will do what it looks like you are trying to do: Run for a fixed amount of time when they press Fire1 and not stop until the time is out:
var maxRunTime = 5.0f; //The amount of time to run
var runSpeed : float = 10.0f; //The speed while running m/s
var speed : float = 5.0f; //The speed while not running m/s
private var runTime : float = 0.0f; //The amount of time left to run
private var controller : CharacterController; //The character controller
private var running : boolean = false; //Are we running?
function Start() {
controller = GetComponent(CharacterController); //Get the controller
}
function Update() { //If time is full and we press Fire1, run
if (Input.GetButtonDown("Fire1") && runTime == maxRunTime) running = true;
var currentSpeed : float = Input.GetAxis("Vertical");
if(running && runTime > 0) { //Run until you can't
runTime -= Time.deltaTime;
currentSpeed *= runSpeed;
if(runTime <= 0) {
runTime = 0;
running = false;
}
}
else { //Not running, so recharge time
runTime += Time.deltaTime * 0.5;
if(runTime > maxRunTime) runTime = maxRunTime;
currentSpeed *= speed;
}
controller.SimpleMove(forward * currentSpeed);
}
@script RequireComponent(CharacterController)
If you didn't intend to run until out of time, but intended to only run when holding Fire1 then you would do something like:
var maxRunTime = 5.0f; //The amount of time to run
var runSpeed : float = 10.0f; //The speed while running m/s
var speed : float = 5.0f; //The speed while not running m/s
private var runTime : float = 0.0f; //The amount of time left to run
private var controller : CharacterController; //The character controller
private var running : boolean = false; //Are we running;
function Start() {
controller = GetComponent(CharacterController); //Get the controller
}
function Update() {
if(Input.GetButton("Fire1")) running = true;
else running = false;
var currentSpeed : float = Input.GetAxis("Vertical");
if(running && runTime > 0) { //Run
runTime -= Time.deltaTime;
currentSpeed *= runSpeed;
}
else { //Don't run
if(!running) runTime += Time.deltaTime * 0.5;
currentSpeed *= speed;
}
runTime = Mathf.Clamp(runTime, 0.0f, maxRunTime);
controller.SimpleMove(forward * currentSpeed);
}
@script RequireComponent(CharacterController)
If you only wanted to be able to start running when the runTime is at max, then you would have to handle button down and button up separately in the above: