- Home /
Delay after Attack Button and Decreasing Mana Bar
Good Afternoon! My name is Abel and I'm a beginer in this world, so I need some help...
I have made a GUI with some buttons, and one of that have the function to decrease the health of a monster, but if you press the button fast, you can kill the monster in less that 1 second, so I want to ask how I can do a delay mecanism that activate itself after pressing the attack button and doesn't allow you to press the button again before the delay finish. I tried to do it with a for that contains a waitForSeconds, inside the button code (if()), but it doesn't work.
And also I have another little question. I have a mana bar that decrease when pressing a button in my game and i want to create a progressive decreasing bar, I mean, I want it to decrease slowly, and not to make the bar smaller instantaneously after pressing the button. Is it clear?
The button is only a if(GUI.Button(Rect(x, y, x, y), "")) States.mana-10;, and there is a mana bar which width is given by this State.mana var.
Thank you a lot! Abel!
Answer by tw1st3d · Oct 03, 2013 at 03:45 PM
Here, try something like this.
using System.Collections;
using UnityEngine;
using System;
public class FightEnemy : MonoBehavior
{
public float attackTime, waitTime = 1.0f;
protected bool canAttack = true;
public void OnMouseDown()
{
attackTime = Time.time;
if(canAttack)
{
Enemy.TakeDamage(1); // This is pseudo code, obviously just an example.
canAttack = false;
}
}
public void Update()
{
if(!canAttack)
{
if(Time.time > attackTime + waitTime)
canAttack = true;
}
}
}
Thank you! It solved the delay part (Well, I translated to js, but it worked okey:)! THAN$$anonymous$$S!
And if you can (Or anyone), can you tell me how to solve the other part? Thank you!
Your mana bar thing? I'd do something like
if(some_coniditonal)
mana = (mana - 1.2) * Time.deltaTime;
Also, if this is correct, would you $$anonymous$$d ticking it?
No, it doesen't work... The first answer is perfect, but this empty my mana bar immediately when I press the $$anonymous$$agic Attack Button.
The idea is that when you press a GUI.Button, the mana bar decreases 10 points (from 100), but one by one, to make add some "graphics effects" to this bar.
Thank you!
Okay, couldn't post this as an answer, so here's what you should do:
$$anonymous$$ake an IEnumerator(A$$anonymous$$A Coroutine) that will decrease your magic by 1 every, say, 0.05 seconds.
using UnityEngine;
using System.Collections;
public void Handle$$anonymous$$agic : $$anonymous$$onoBehavior
{
protected int magic;
public void Start()
{
magic = 100;
}
public void Update()
{
if(Input.Get$$anonymous$$eyDown("$$anonymous$$agic Attack Button"))
{
if(magic > 9)
StartCoroutine( Handle() );
else
// Too little magic
}
}
public IEnumerator Handle()
{
int left_to_decrease = 10;
while(left_to_decrease > 0)
{
magic--;
yield return new WaitForSeconds(0.05f);
left_to_decrease--;
}
yield return null;
}
}
I sopouse it will work since it seems logic, but what yeld is used for? I'm doing the scripts in js and the two lines with yeld cause errors. Is posible to use this on Js? thanks!