Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Coding an 2D action adventure game's combat

Discussion in 'Scripting' started by lylat, Jun 11, 2013.

  1. lylat

    lylat

    Joined:
    Sep 12, 2012
    Posts:
    28
    Hi Guys,
    I've been lurking these forums for a long time now but it's my first time actually making the effort to post my own question.

    Now to the problem...

    Another and myself are trying to create an action adventure game with melee fighting and there are only 2 of us actually working on it, him on the art and myself on game design and programming. I come with questions about how to go about coding a 2d combat game. I don't need to know specifics about coding I'm familiar enough. I'm talking about how would i go about doing the collisions properly, what kind of system would I use for the combo's and what would be the best way to make sure that this is still possible to code as a single person while still being efficient.

    Now the catch, each of the actions that can be done(combos/attacks) can be done in 3 different states(to the same effect) this is where a state machine gets tricky for me. I've thrown away 20 ideas and started from scratch 3 times each time thinking I had it then realizing it was stupid.

    Thanks in Advance

    ~Lylat
     
  2. mark-velthuis

    mark-velthuis

    Joined:
    Feb 10, 2011
    Posts:
    47
    Can you perhaps post 1 or 2 of your ideas. Specify what the reasons were for throwing them away (or why you thought they were stupid).
     
  3. lylat

    lylat

    Joined:
    Sep 12, 2012
    Posts:
    28
    Thanks for replying Mark.


    Well Initially I was using a system that would register directions of the input axis then combine those with attack inputs to make combos, see streetfighter, however the result was not looking like what I wanted not to mention the code became incomprehensible even to me with the amount of if statements involved that I just had to start again.

    Another time i was using a state machine to make sure that each input would begin the state associated with the first input. Slowly I realised that I wouldn't be able to link commands using this and it was scrapped again.

    Now I'm using a state machine again however, I'm setting out the combat in stages of attack where:
    Stage 1
    Input 1 or 2
    Stage 2
    Input 1 or 2
    Stage 3
    Input 1 or 2

    etc. and there will be different results on each stage depending on the result of the preceeding stage.

    This one I think gives me most hope however I'm still not confident i'm going about it the right way because it seems WAAAYYY too hard to get even the most simplest of things to work correctly.
     
  4. mark-velthuis

    mark-velthuis

    Joined:
    Feb 10, 2011
    Posts:
    47
    Hmm, this actually looks quite a lot like a weapon switch system I tried once. My solution worked quite well for what I wanted to do, I'm not sure how it translates to a fighter since my solution wasn't time based nor did it take attack animations into account.
    this is roughly how it worked :

    I had a State class.
    a state had 3 arrays. Weapons, States, Inputs.
    and a string that was responsible for containing the most recent user inputs (up to a certain length)

    Every time input was registered, it would check the inputs array to see if the most recent inputs matched any of them.
    If it did, it would activate the corresponding weapon and go to the corresponding state.

    This systems requires very little in actual code because it's content driven. Just add more states, weapons and inputs, no need to change code.
    It's infinitely recursive, the combo can go on as long as you'd want (do make sure there is an ending or reset point).

    I can try to find the source code, allthough I'm afraid it might be lost.
     
  5. lylat

    lylat

    Joined:
    Sep 12, 2012
    Posts:
    28
    Never thought to do it using arrays and strings to record input, How would you go about using the strings to make sure that an input was read?

    Would it be something like:

    Code (csharp):
    1. if (array[1] == "attackbutton"){
    2. array.Shift;
    3. //functionality
    4. }
    5.  
    6. if(Input(attackbutton)){
    7. Array.Push = "attackbutton";
    8. }
    9.  
    10.  
    Thanks again :D
     
  6. mark-velthuis

    mark-velthuis

    Joined:
    Feb 10, 2011
    Posts:
    47
    I couldn't find the exact code. but it looked something like this pseudo code

    Keep in mind that his is designed with sequence input in mind. Like the fireballs and stuff in Street fighter. Not so much the combo attacks that you see in dead or alive and soul calibur. Allthough they should work aswell.
    Code (csharp):
    1.  
    2.  
    3. string RecentInputs;
    4.  
    5. If (PressUpKey){
    6.     RecentInputs = RecentInputs + "u";
    7. }
    8. If (PressDownKey){
    9.     RecentInputs = RecentInputs + "d";
    10. }
    11. If (PressLeftKey){
    12.     RecentInputs = RecentInputs + "l";
    13. }
    14. If (PressRightKey){
    15.     RecentInputs = RecentInputs + "r";
    16. }
    17. If (PressAttackKey){
    18.     RecentInputs = RecentInputs + "a";
    19. }
    20.  
    21. For each inputcommand in inputcommands array
    22.    if RecentInputs ends with inputcommand, do stuff.
    23.  
    24.  
    It's probably not the most efficient way to handle this. There's also a few flaws with the system that may or may not apply to your situation.
     
  7. lylat

    lylat

    Joined:
    Sep 12, 2012
    Posts:
    28
    Thanks so much for helping. I'll see what shenanigans I can get up to with this new system. Hopefully I should be able to get it working :D

    Again, Thanks for helping. I've been pulling my hair out for a while before having a relaxing conversation proving its possible haha.

    ~Lyl
     
  8. mark-velthuis

    mark-velthuis

    Joined:
    Feb 10, 2011
    Posts:
    47
    No problem. Let me know how it turns out and if you've made any improvements on the system.
     
unityunity