- Home /
Question by
Asuzu11 · Jul 20, 2021 at 11:39 AM ·
movement scriptstamina
Stamina doesnt always activate
Im trying to make a stamin system in my game but it doesnt really work. I am able to run by hitting shift which is the sprint button, but the stamina doesn't deplete until a few secoinds later. It sometimes doesn't activate at all. How can I fix this?
{
// groundCheck
public Transform groundCheck;
public float groundDistance = 1;
public LayerMask groundMask;
private bool isGrounded;
//move
public CharacterController controller;
public float speed = 8;
Vector3 velocity;
public float gravity = -9.81f;
public float jumpHeight;
//health and stamina
public Slider HealthBar;
public Slider StaminaBar;
public int currentStamina;
public int maxStamina = 100;
public int maxHealth = 100;
public int currentHealth;
public Stamina stamina;
public bool ifRun = false;
public bool ifWalk = true;
void Start()
{
currentStamina = maxStamina;
stamina.SetMaxStamina(maxStamina);
currentHealth = maxHealth;
}
void Update()
{
//gravity
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
//move
float X = Input.GetAxis("Horizontal");
float Z = Input.GetAxis("Vertical");
Vector3 move = transform.right * X + transform.forward * Z;
controller.Move(move* speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//sprint
if (X > 0 || X < 0 || Z < 1 || Z > -1)
{
ifWalk = true;
}
if (ifWalk == true && Input.GetKey("left shift"))
{
UseStamina(2);
speed = 10;
ifRun = true;
}
else if (Input.GetKeyUp("left shift"))
{
speed = 5;
ifRun = false;
}
if (ifRun == false)
{
UseStamina(-5);
}
//jump
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
}
public void UseStamina(int amount)
{
if (currentStamina - amount >= 0)
{
currentStamina -= amount;
stamina.setStamina(currentStamina);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Stamina cooldown 5 Answers
Respawn Problems 0 Answers
Waypoint pathfinding gone terribly wrong. 1 Answer
Help with movement script? 0 Answers
Rolling ball 2 Answers