- Home /
Trouble with Stamina controller
This time I was following the steps of a tutorial (https://youtu.be/teaMf4hC16k) to make an Health / Mana / Stamina bar in my game, and I have two problems. The first problem is that the "&&" logic gate doesn't work, and the Stamina falls only pressing "LShift. The second is that when Stamina goes 0, the character can continue sprinting.I revised the whole code for 2 hours and i haven't found the issue. Here's the whole code:
var currentHealth : float = 39; var maxHealth : int = 39;
var currentMana : float = 50.0; var maxMana : int = 50;
var currentStamina : float = 50.0; var maxStamina : float = 50;
var barLength = 0.0;
private var chMotor : CharacterMotor;
function Start () { barLength = Screen.width / 8; chMotor = GetComponent(CharacterMotor); }
function Update () { AdjustCurrentHealth (0); AdjustCurrentMana (0);
/*MANA CONTROL SECTION*/
//Normal Mana Regeneration
if (currentMana >= 0) {
currentMana += Time.deltaTime * 2;
}
//Don't let Mana go above 100
if (currentMana >= maxMana) {
currentMana = maxMana;
}
//Don't let Mana go below 0
if (currentMana <= 0) {
currentMana = 0;
}
//When does Mana decrease?
if (Input.GetKeyDown("q")) {
AdjustCurrentMana (-20);
}
if (Input.GetKeyDown("e")) {
AdjustCurrentMana (-25);
}
/*STAMINA CONTROL SECTION*/
//Set and find Character Controller
var controller : CharacterController = GetComponent (CharacterController);
//Reduce Stamina bar when pressing LShift and moving
if (controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift)) {
currentStamina -= Time.deltaTime * 10;
chMotor.movement.maxForwardSpeed = 10;
chMotor.movement.maxSidewaysSpeed = 10;
}
//Normal speed when not pressing anything
else {
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
//Stamina regeneration
if (controller.velocity.magnitude == 0 && (currentStamina >= 0)) {
currentStamina += Time.deltaTime * 10;
}
//Don't let sprint when Stamina is less than 0
if (controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift) && currentStamina <= 0) {
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
//Don't let Stamina go above 100
if (currentStamina >= maxStamina) {
currentStamina = maxStamina;
}
//Don't let Stamina go below 0
if (currentStamina <= 0) {
currentStamina = 0;
}
}
function OnGUI () { //Icons for GUI GUI.Box(new Rect(5, 30, 40, 20), "HP"); GUI.Box(new Rect(5, 50, 40, 20), "Mana"); GUI.Box(new Rect(5, 70, 40, 20), "Stam");
//Health / Mana / Stamina main bars
GUI.Box(new Rect(45, 30, barLength, 20), currentHealth.ToString("0") + "/" + maxHealth);
GUI.Box(new Rect(45, 50, barLength, 20), currentMana.ToString("0") + "/" + maxMana);
GUI.Box(new Rect(45, 70, barLength, 20), currentStamina.ToString("0") + "/" + maxStamina);
}
function AdjustCurrentHealth (adj) { currentHealth += adj;
if(currentHealth >= maxHealth)
{
currentHealth = maxHealth;
}
if (currentHealth <= 0)
{
currentHealth = 0;
}
}
function AdjustCurrentMana (adj) { currentMana += adj; }
And this is the Stamina Section:
/STAMINA CONTROL SECTION/
//Set and find Character Controller
var controller : CharacterController = GetComponent (CharacterController);
//Reduce Stamina bar when pressing LShift and moving
if (controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift)) {
currentStamina -= Time.deltaTime * 10;
chMotor.movement.maxForwardSpeed = 10;
chMotor.movement.maxSidewaysSpeed = 10;
}
//Normal speed when not pressing anything
else {
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
//Stamina regeneration
if (controller.velocity.magnitude == 0 && (currentStamina >= 0)) {
currentStamina += Time.deltaTime * 10;
}
//Don't let sprint when Stamina is less than 0
if (controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift) && currentStamina <= 0) {
chMotor.movement.maxForwardSpeed = 6;
chMotor.movement.maxSidewaysSpeed = 6;
}
//Don't let Stamina go above 100
if (currentStamina >= maxStamina) {
currentStamina = maxStamina;
}
//Don't let Stamina go below 0
if (currentStamina <= 0) {
currentStamina = 0;
}
}
Any ideas to solve this problem? Thanks in advance!
Answer by meat5000 · Jul 30, 2015 at 02:53 PM
It works without fail.
Try enclosing your conditions in brackets
if(x > y || g < h && g == x)
if((x > y) || (g < h) && (g == x))
If the leftmost condition fails the if should not run. Use Debug.Log to check these conditions.
Add a velocity deadzone. Float values have inaccuracy and its very likely that '> 0' is a condition that is always true.
controller.velocity.magnitude > 0.1 && //etc
I've tried the 2 options and still not working for me. If that's important, the Character controller attached to it is the default Unity's Asset Package, and to de character are attached "Character $$anonymous$$otor", "Third Person Controller" and "Third Person Camera"
Your answer
![](https://koobas.hobune.stream/wayback/20220611233719im_/https://answers.unity.com/themes/thub/images/avi.jpg)