Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by abelreyes · Oct 03, 2013 at 03:26 PM · delayprogress-bar

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
         }
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abelreyes · Oct 03, 2013 at 07:30 PM 0
Share

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!

avatar image tw1st3d · Oct 03, 2013 at 08:11 PM 0
Share

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?

avatar image abelreyes · Oct 03, 2013 at 08:22 PM 0
Share

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!

avatar image tw1st3d · Oct 03, 2013 at 08:31 PM 0
Share

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;
     }
 }
avatar image abelreyes · Oct 03, 2013 at 08:42 PM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to reset health/energy/progress bars and how to show the ammo in numbers? 1 Answer

How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers

android audio delayed 1 Answer

cooldown timers 0 Answers

Shoot Bullet Delay Enemy 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges