# How do I add a tick of sorts?

So my issue is that I can’t think if a solution that allows me to have a tick of sorts that says when im changing directions or not sprint my stamina hast to wait so long to start regenerating, if anyone can help me our that would be awesome here is my script so you know what I’m doing, if you see anything else i can improve in terms of my regen let me know thank you.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
public float walkSpeed = 3f;
public float runSpeed = 5f;
public Transform movePoint;

Stats playerStats;

public bool isSprinting;

public float lastRegen;
public float staminaRegenSpeed = 1.25f;
public float staminaRegenAmount = 1f;

// Start is called before the first frame update
void Start()
{
playerStats = GetComponent<Stats>();
movePoint.parent = null;
}

private void Update()
{
Sprint();
RegenStamina();
}

// Update is called once per frame
void FixedUpdate()
{
transform.position = Vector3.MoveTowards(transform.position, movePoint.position, walkSpeed * Time.deltaTime);

if (Vector3.Distance(transform.position, movePoint.position) == 0f)
{
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f), .2f, barrier))
{
movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0f, 0f);
}

}
else if (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1)
{
if (!Physics2D.OverlapCircle(movePoint.position + new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f), .2f, barrier))
{
movePoint.position += new Vector3(0f, Input.GetAxisRaw("Vertical"), 0f);
}

}
}
}

public void Sprint()
{
if (Input.GetKey(KeyCode.LeftShift) && (Mathf.Abs(Input.GetAxisRaw("Vertical")) == 1) | (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1))
{
walkSpeed = runSpeed;
isSprinting = true;
playerStats.currentStamina -= 1f * Time.deltaTime;
playerStats.CheckStamina();
}
else
{
walkSpeed = 3f;
isSprinting = false;
}
}

public void RegenStamina()
{
if (Time.time - lastRegen > staminaRegenSpeed && !isSprinting && playerStats.currentStamina < playerStats.maxStamina)
{
playerStats.currentStamina += staminaRegenAmount;
lastRegen = Time.time;
playerStats.CheckStamina();
}
}
}
``````

You have to update lastRegen for every frame that you are sprinting.

Quick fix (I believe):

``````isSprinting = true;
playerStats.currentStamina -= 1f * Time.deltaTime;
lastRegen = Time.time; // <---- Added
``````

But if you do that, the name of the variable (lastRegen) will no longer make sense. Maybe try something along the lines of regenCooldownTimestamp (I chose Timestamp instead of Time because it’s a point in time, not how many seconds).

Personally, I prefer to always take into account that I might add a pause feature later. Because of this, I prefer to use a countdown timer instead of timestamps.

Which means that each frame I decrease a variable called cooldown by Time.deltaTime.

If an action occurs that should block stamina regeneration (like sprinting, climbing ladders, etc), I reset the cooldown to the max value.

After the actions have been performed, if the cooldown is allowed to reach 0 I regenerate a bit of stamina.

This way, if I implement a pause feature the player will not be able to cheat by pausing the game for a couple of seconds to skip the cooldown.