- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
StarsideStudios · Jul 31, 2014 at 06:40 AM ·
runcrouchsprint
How do I add a sprint recharge
I have a basic sprint script that allows the player to walk using "W" run using "Shift" and Crouch using "c". As the player sprints the stamina goes down. And it stays down. I want it to go back up to 100 so the player can sprint again. here is the script
var walkSpeed: float = 8;
var crchSpeed: float = 3;
var runSpeed: float = 16;
var stamina : int = 100;
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float;
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")) {
if (stamina != 0) {
speed = runSpeed;
stamina = stamina - 1;
}
}
if (Input.GetKey("c") || Input.GetKey("c")) {
h = 0.5 * height;
speed = crchSpeed;
}
chMotor.movement.maxForwardSpeed = speed;
var lastHeight = ch.height;
ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2;
if (stamina != 100) {
stamina = stamina + 0.5;
}
}
function OnGUI () {
GUI.Box(Rect(120,Screen.height - 50, 80, 40), "");
GUI.Label(Rect(130,Screen.height - 30, 180, 125), stamina + " / " + "100");
GUI.Label(Rect(130,Screen.height - 50, 180, 125), "Stamina");
}
Comment
every time you type a code just select it then press the 101010 button so it can be read
Answer by Arcadewiz · Jul 31, 2014 at 07:23 AM
In line 36, try :
if(stamina < 100)
instead of :
if(stamina != 100)
Your answer
![](https://koobas.hobune.stream/wayback/20220613155438im_/https://answers.unity.com/themes/thub/images/avi.jpg)