- Home /
The question is answered, right answer was accepted
Get Key(KeyCode)???
So my question is how do I slow down an int so if im taking away or adding to somethig.. because when i run my stamina goes ywa to fast cuase the get key stacks same with taking damage or adding health. The health is still being worked on thats why im using key codes.
public void Update()
{
if (Input.GetKey(KeyCode.P))
{
TakeDamage(2);
}
if (Input.GetKey(KeyCode.L))
{
AddHealth(50);
}
}
public void TakeDamage(int damage)
{
if(currentHealth - damage >= 0)
{
currentHealth -= damage;
healthBar.value = currentHealth;
}
}
public void AddHealth(int heal)
{
if(currentHealth + heal >= 0)
{
currentHealth += heal;
healthBar.value = currentHealth;
}
}
// Stamina script
public void UseStamina( int amount)
{
if(currentStamina - amount >= 0)
{
currentStamina -= amount;
staminaBar.value = currentStamina;
if(regen != null)
{
StopCoroutine(regen);
}
regen = StartCoroutine(RegenStamina());
}
}
Answer by lcplrice · Oct 18, 2020 at 08:53 PM
GetKey
runs as long as its held down, so if your running 60 FPS it fires 60 times. Try GetKeyDown
it only runs once the key is fully pressed and only once.
with stamina try using : currentStamina -= amount * Time.DeltaTime
this will help slow it down based on the frame rate.
Follow this Question
Related Questions
Movement speed doesn't change with variable C# 1 Answer
Movement of increased certain speed type not registering. 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Moving a player with Rigidbody 2 Answers