- Home /
Question by
melvincostner · Jun 12, 2021 at 11:56 AM ·
buttonrigidbody2dfunctiontimerboolean
determine the minimum time for the player to hold the button
the idea is that when the player holds the heal button, the character stays still and concentrating the healing (as in the hollow knight), and spends part of the collected souls, i have been trying something like this.
public void SoulsControl()
{
if (souls >= maxSouls)
{
souls = maxSouls;
}
if (playerInputs.healing && souls > healCust)
{
canMove = true;
rb.velocity = Vector2.zero;
souls -= healCust;
isHealing = true;
if (isHealing)
{
StartCoroutine(GainHealth());
}
else
{
StopCoroutine(GainHealth());
canMove = false;
isHealing = false;
}
}
}
IEnumerator GainHealth()
{
yield return new WaitForSeconds(timeBtwHeal);
Damageable damageable = GetComponent<Damageable>();
damageable.SetHealing(1);
isHealing = false;
canMove = false;
}
Comment
I don't know if this is what you mean. I thinking that you want to record how long did the player held the button.
IEnumerator GainHealth()
{
float holdDuration = 0;
while(holdDuration <= timeBtwHeal && isButtonHeld){ // "isButtonHeld" is what ever input youre using here, just change it.
holdDuration += Time.deltaTime;
yield return null;
}
Debug.Log("the player held the button for " + holdDuration +" seconds");
Damageable damageable = GetComponent<Damageable>();
damageable.SetHealing(1);
isHealing = false;
canMove = false;
}
Best Answer
Answer by PhyrePhrost · Jun 12, 2021 at 05:29 PM
You can use a function that set a boolean value to true... And a timer into the update function, when the boolean value is true the timer start... when passing certain seconds you will call the method
public bool startTimer = false;
float timer = 0;
public float timeToReach = 0.3f;
void Update()
{
if(startTimer)
{
if(timer<timeToReach)
timer+=Time.deltaTime;
else
SoulsControl();
}
}
public void OnButtonPressed()
{
startTimer=true;
}
public void OnButtonReleased()
{
startTimer=false;
timer = 0;
}