I'm trying to make a 2d platformer and a really long time ago I added sprinting and it was working perfectly. yesterday it randomly stopped working and I have no idea why so I tried to test if my speed and current stamina values were changing when I held down shift by turning off maximize on play and selecting my player when holding down shift. when I tested it the values were changing and for some reason my sprint was working, but when I stopped selecting my player and selected something else, it stopped letting me sprint. I don't think this is a bug with my code I just think this is a unity glitch but I'll include my code anyways
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerContoller : MonoBehaviour
{
private Rigidbody2D rb;
public float speed = 7.34f;
public float jumpForce;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public LayerMask Slow;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
public int maxStamina = 163;
public int currentStamina;
public bool canSprint;
public Stamina stamina;
private Animator anim;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
currentStamina = maxStamina;
stamina.SetMaxStamina(maxStamina);
}
private void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
if(moveInput == 0)
{
anim.SetBool("isRunning", false);
} else
{
anim.SetBool("isRunning", true);
}
if (currentStamina > 0)
{
canSprint = true;
} else if (currentStamina < 0)
{
canSprint = false;
}
if (currentStamina < maxStamina)
{
RegenStamina(2);
}
if (canSprint == true)
{
if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftShift))
{
LoseStamina(5);
speed = 14f;
}
} else
{
speed = 7.34f;
}
if (canSprint == false)
{
speed = 7.34f;
}
}
private void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
anim.SetTrigger("takeOff");
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (isGrounded == true)
{
anim.SetBool("isJumping", false);
}
else
{
anim.SetBool("isJumping", true);
}
if(Input.GetKey(KeyCode.Space) && isJumping == true)
{
if(jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
}
}
void LoseStamina(int StaminaGone)
{
currentStamina -= StaminaGone;
stamina.SetStamina(currentStamina);
}
void RegenStamina(int AddStamina)
{
currentStamina += AddStamina;
stamina.SetStamina(currentStamina);
}
}
and in a different script I have
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Stamina : MonoBehaviour
{
public Slider slider;
public void SetMaxStamina(int Stamina)
{
slider.maxValue = Stamina;
slider.value = Stamina;
}
public void SetStamina(int Stamina)
{
slider.value = Stamina;
}
}
(this is to make the slider move)
Since your max stamina is 163 and you reduce 5 stamina every FixedUpdate
it is very likely that you won't experience the sprinting effect for long enough to notice it (~0.4s) assuming you did not change the default fix update rate from 0.02 (50 updates per second).
You can try increasing the stamina value to test it out or reduce the stamina consumption every execution of the FixedUpdate
method.
If you want to do it in FixedUpdate
, try the following:
LoseStamina(5 * Time.fixedDeltaTime);
This will change the stamina consumption to 5 per second, you can tweak it as you see fit.