- Home /
Question by
swipe2000 · Mar 12, 2017 at 05:33 PM ·
variablesmovement scriptfps controllercooldownsprinting
Stamina script system
Hi, I'm trying to make a stamina system for a FPS controller I'm making but it wont work. Unity says the variable running and StartReloadingStamina exists but isnt in use, which doesnt make sense. This breaks the running system and pretty much allows you to run forever. Thanks to whoever can fix it for me :)
P.S: If you could put comments within the code to show me where I made the error, I would be greatful.
{
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
bool running;
bool StartReloadingStamina = true;
private const float _waitTime = 5;
private Vector3 moveDirection = Vector3.zero;
float stamina = 5;
///Variables within script
IEnumerator Cooldown()
{
StartReloadingStamina = false;
// wait for set amount of time
yield return new WaitForSeconds(_waitTime);
StartReloadingStamina = true;
}
///Above is Stamina Cooldown
///
void Update()
{
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if (stamina > 0)
{
running = true;
speed = 8.0F;
stamina -= Time.deltaTime;
if (stamina < 0)
{
speed = 6.0F;
running = false;
StartCoroutine(Cooldown());
}
}
}
///GKU is when key is released
if (Input.GetKeyUp(KeyCode.LeftShift))
{
running = false;
StartCoroutine(Cooldown());
speed = 6.0F;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
///Uses character controller for physics.
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make sprinting 1 Answer
Sprint script help, I keep through the floor 0 Answers
Sprint and Crouch problem! 1 Answer
Add a Charger/Cooldown system to a movement script for a 2D Top-Down Shooter. 1 Answer
Help with cooldown timer 0 Answers