- Home /
Is this sprint/stamina script optimal or wasting performace?
is this good or is the constantly running IEnumerator a waste of performace?
void Update()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
if(canSprint)
toggleSprint();
}
}
IEnumerator running() // SPRINTNIG
{
while (true)
{
if ((!isSprint||rigidbody.velocity.magnitude <= 0.1f) && stamina<=maxstam)
{
stamina += 1; // regen
}
else if (isSprint && stamina >= 1)
{
stamina -= 3;
if (stamina < 1)
{
canSprint = false;
toggleSprint();
yield return new WaitForSeconds(2.5f); // exhausted cooldown
canSprint = true;
}
}
yield return new WaitForSeconds(.1f);
}
}
void toggleSprint()
{
isSprint = !isSprint;
speed = (isSprint) ? 10 : 5;
}
Comment
Your answer
Follow this Question
Related Questions
Aligning animation with movement. 0 Answers
Slime script? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Simple 2D Enemy AI 3 Answers
Integration tests fail on 'Run All' 2 Answers