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;
public LayerMask barrier;
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();
}
}
}