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 /
This question was closed Jun 02, 2012 at 09:13 PM by SrBilyon for the following reason:

Solved

avatar image
0
Question by SrBilyon · May 13, 2012 at 01:22 PM · combofightingfsmfinite-state-machine

Creating a Branching Combo System using a FSM

I have traditionally wrote combos for battle systems using a series of compound if-statements and switch cases, but that tends to build up and become a mess at times.

As of my most recent implementation, I have three attack buttons: Soft Medium Heavy

Now, the attacks are stored in individual classes that contain attack info such as: Attack strength Speed Knockback Force Launch Force Animation

In Update, I have a switch case that increments as I press an attack key (each press is it's own attack). For each case in the switch, it calls the function that's in it, and the function replaces the global variables with the matching names that are in the class (i.e Attack.attackStr = attackStr)

Now I was thinking of using a finite state machine.

I basically want it so that when i hit the standard attack say, 3 times, and hit the medium button next (the fourth blow), it knows to continue the combo using that button or finish the combo off, etc (while checking to see if the player is in the air or holding the "x button".

Is there an easier way to make a branching combo system using minimal compound statements? Thanks in advance!

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

  • Sort: 
avatar image
2
Best Answer

Answer by whydoidoit · May 13, 2012 at 11:50 PM

Perhaps you should put your button handling in the attack classes (though I may have mistaken what you are after). This would create an FSM as each press of the button would effectively change the state to the next attack.

To do that you would pass the handling of player input to a function in the current attack class (or a NoAttack if this is the first move).

The function you called would return the next attack so that you are ready the next time the player presses a button. If there was no user input you just return the current state.

Pseudo code:

 public class Attack
 {
      public virtual Attack HandleUserInput() { return this; }
      public bool performImmediately;
 }
 public class NoAttack : Attack
 {
     public override Attack HandleUserInput()
     {
           switch(...) //Some kind of player input step
           {
                case ...:
                     return attackMove1; //Move through the combo if 'x' pressed
                case ...:
                     return attackMove2; //Another state if 'y' pressed
           }
           return this;
     }
 }
 public NoAttack noAttack = new NoAttack();
 public AttackMove1 attackMove1 = new AttackMove1();
 ...
 public Attack currentAttack = noAttack;
 ...
 float attackStartTime = 0f;
 void Update()
 {
     currentAttack = currentAttack.HandleUserInput();
     if(currentAttack.performImmediately)
     {
        Dosomething(currentAttack);
        currentAttack = noAttack;
        attackStartTime = 0f;
     }
 
     if(currentAttack != noAttack && attackStartTime == 0)
        attackStartTime = Time.time;
 
     //You would probably combine this with the block above
     //split here for clarity
     if(currentAttack != noAttack && Time.time - attackStartTime > someValue)
     {
        Dosomething(currentAttack);
        currentAttack = noAttack;
        attackStartTime = 0f;
     }
  
 
 }

Where attackMove1, attackMove2 etc are instances of classes that handle that stage of the combo and are also inherited from Attack.

Presumably at some point an attack would have performImmediately set and that's what you would do, alternatively an Update function might wait for a time out and then implement the attack that was currently selected. In either case you would return the variable containing the current attack to an instance of NoAttack after selecting the action you desire.

This is nice because all of your logic is neatly encapsulated in an class that can easily be swapped around, unlike complicated switch and if combinations.

You would call the HandleUserInput of currentAttack in your Update function.

While this provides you a basic state machine - it doesn't do a bunch of "State Machine" like functions such as handling state entry and exit - but I think it might be enough for what you are after.

Comment
Add comment · Show 4 · 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 whydoidoit · May 14, 2012 at 12:10 AM 0
Share

You could also have a NoAttackInTheAir etc...

avatar image SirGive · May 14, 2012 at 01:14 AM 0
Share

I think he's looking for a solution that will be easily maintainable... This one looks like it might end up code spaghetti.

avatar image whydoidoit · May 14, 2012 at 01:18 AM 0
Share

Really? I guess it's down to what you like. I like encapsulated classes that are easily documented and separated. He already has a class for each of the attacks. I'd rather that than a 100 line embedded switch statement. It's all down to style I suppose :)

avatar image SirGive · May 14, 2012 at 01:39 AM 0
Share

That was actually my first suggestion as well, but apparently its all the same functionality with just different values. If the attacks were as complicated as $$anonymous$$H though, I'd personally separate them into files. That way you aren't searching through one file for different attacks.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Unable to stop excess input and unable to call the entered combo from the combo system 0 Answers

How to make a continuous "combo" system 2 Answers

Combo Script Problem 1 Answer

enemy reacting to player 1 Answer

How to Make Two Variables Indirectly Related? 2 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