So I have a stamina bar but it's wonk AF. Lets you keep sprinting when stamina is zero if you don't let go of the button, and takes a few seconds to start refilling the bar for no reason I understand. Code (CSharp): if (characterController.isGrounded && Input.GetButtonDown("Sprint") && groundSlopeAngle < slopeLimit && playerStamina > 0) { isSprinting = true; } if (characterController.isGrounded && Input.GetButtonUp("Sprint") || playerStamina == 0) { isSprinting = false; } if (isSprinting) { TakeStamina(0.5f); movementSpeed = sprintSpeed; } if (!isSprinting) { movementSpeed = walkSpeed; if (playerStamina < 100) { AddStamina(0.4f); } if (playerStamina >= 100) { playerStamina = 100; sliderStamina.value = playerStamina; } } MoveDir = (desiredMoveDirection * movementSpeed); MoveDir.y = ySpeed; jumpDelay -= Time.deltaTime; } public void TakeStamina(float amount) { playerStamina -= amount; sliderStamina.value = playerStamina; } public void AddStamina(float amount) { playerStamina += amount; sliderStamina.value = playerStamina; }
A couple of notes: - I assume the first lines belong to Update() - TakeStamina and AddStamina should be moderated by Time.DeltaTime if they are invoked in Update() - Check your guards and use parantheses. Are you sure that the || is evaluated correctly versus the other "&&"? - jumpDelay, moveDir have no function. If you edit out stuff, make sure you edit out everything, but it's usually better to leave in everything.
Your playerStamina will rarely be exact 0. You should not use "==" when comparing floats. Better use " playerStamina < 0" Tip for the future. If you want to know, if a float has a given value: Code (CSharp): float stamina; float boarderValue; float tolerance; if(Mathf.abs(stamina-boarderValue) < tolerance) { //... }