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
1
Question by AlejandroGorgal · Dec 01, 2013 at 12:21 AM · timejavaswitch

How can I use time to reset a switch statement?

Hello, Im trying to setup a combo system for my game where the player will be able to chain attacks if the attack button is pressed in rapid succession.

I've been experimenting with switch statements and it seems like it'll be the way to go, here's a simple test code I made:

 var comboCount: int = 0;
 
 function Update () 
 {
     if (Input.GetKeyDown(KeyCode.Space))
         comboFunction();
 }
 
 function comboFunction ()
 {
     switch (comboCount)
     {
         case 0:
             print ("Idle");
             comboCount += 1;
             break;
         case 1:
             print ("Attack1");
             comboCount += 1;
             break;
         case 2:
             print ("Attack2");
             comboCount += 1;
             break;
         case 3:
             print ("Attack3");
             comboCount = 0;
             break;
     }
 }

So, what Im doing here is printing a different message each time the space key is pressed. The first time it'll return "Idle", then "Attack1", "Attack2" and finally "Attack3" then go back to idle.

So what I want to do now is implement a timer so that if there are no key presses during X amount of time (let's call it "var reFireTime") then the "comboCount" variable will be reset to zero.

Could anyone explain how to do this? I've been reading up on Time.time but Im struggling in figuring out how to implement this to my example.

Thank you for your time.

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
1
Best Answer

Answer by bompi88 · Dec 01, 2013 at 02:20 AM

This method I show you here is a basic and simple way to do it. It's btw not tested at any level. You can call a timer function right after your combo function, but then your program will probably register several clicks at "the same time". In other words, you will do all of the states Idle, Attack1, Attack2 and Attack3 in matter of milliseconds. You are statistically not quick enough to release your finger off the button. You have to lock the button for an amount of time, lets say for about 0.5s. This is the minimum time you have to wait before the player can do another attack. So lets implement this somehow, by first changing the update function to:

 var isSpaceLocked : boolean = false;
 
 function Update ()  {
     if (Input.GetKeyDown(KeyCode.Space) && !isSpaceLocked) {
        comboFunction();
        StartCoroutine(LockSpaceForTime(0.5f));
        StartCoroutine(TimeCombo());
     } }

Lets implement our LockSpaceForTime(), which takes care of the minimum time you have to wait before a click is registered as a new click:

 function LockSpaceForTime(time : float) {
     isSpaceLocked = true;
     yield WaitForSeconds(time);
     isSpaceLocked = false;
 }

Then implement the maximum time before the combo gets outdated, TimeCombo():

 var timeBeforeComboEnds : float = 1.5f;
 var comboCoroutinesRunning : int = 0;

 function TimeCombo() {
     comboCoroutinesRunning += 1;
     yield WaitForSeconds(timeBeforeComboEnds);
     comboCoroutinesRunning -= 1;
     if (comboCoroutinesRunning == 0)
         comboCount = 0;
 }


So the total script would be:

 var comboCount: int = 0;
 var comboCoroutinesRunning : int = 0;
 var isSpaceLocked : boolean = false;
 var timeBeforeComboEnds : float = 1.5f;
 
 function Update () 
 {
     if (Input.GetKeyDown(KeyCode.Space) && !isSpaceLocked) {
        comboFunction();
        StartCoroutine(LockSpaceForTime(0.5f));
        StartCoroutine(TimeCombo());
     }
 }
 
 function TimeCombo() {
     comboCoroutinesRunning += 1;
     yield WaitForSeconds(timeBeforeComboEnds);
     comboCoroutinesRunning -= 1;
     if (comboCoroutinesRunning == 0)
         comboCount = 0;
 }
 
 function LockSpaceForTime(time : float) {
     isSpaceLocked = true;
     yield WaitForSeconds(time);
     isSpaceLocked = false;
 }
 
 function comboFunction ()
 {
     switch (comboCount)
     {
        case 0:
          print ("Idle");
          comboCount += 1;
          break;
        case 1:
          print ("Attack1");
          comboCount += 1;
          break;
        case 2:
          print ("Attack2");
          comboCount += 1;
          break;
        case 3:
          print ("Attack3");
          comboCount = 0;
          break;
     }
 }

You can change the timing values to whatever that fits your needs, I just chose some almost arbitrary numbers.

Comment
Add comment · Show 5 · 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 AlejandroGorgal · Dec 01, 2013 at 02:34 AM 0
Share

First of all thanks a bunch for the help. Now, using this code Im getting an error:

 No appropriate version of 'UnityEngine.$$anonymous$$onoBehaviour.StartCoroutine' for the argument list '(void)' was found.

Im getting this for both StartCourutine statements, could it be because they are being called inside the Update function?

avatar image bompi88 · Dec 01, 2013 at 02:42 AM 1
Share

just, forgot some yields inside there. How about now?

avatar image bompi88 · Dec 01, 2013 at 02:46 AM 1
Share

Btw, don't take this as a great implementation. I cross my fingers for it to work, right now :P $$anonymous$$aybe some other dude will post a much better solution.

avatar image AlejandroGorgal · Dec 01, 2013 at 02:57 AM 0
Share

It seems to be working now, thanks a lot! This will help me out a great deal :)

avatar image bompi88 · Dec 01, 2013 at 02:58 AM 1
Share

great! if problems, don't hesitate to ask.

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

17 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 avatar image avatar image

Related Questions

how to find time of a button pressed 2 Answers

switching turns between two game objects. 1 Answer

Timer help please 1 Answer

HighScore not updating when the game time is up... 1 Answer

Time formatting 3 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