Search Unity

Simple Combo Combat System

Discussion in 'Scripting' started by Corva-Nocta, Feb 9, 2016.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Alright I've got a combat system I want to use for my game that utilizes button presses at specific times. I am asking for help but also just using this post to get the ideas out of my head and onto paper. I have done research but no one seems to have quite what I am looking for so I'm trying to see if anyone here can help.

    *added note*
    I can not use the mechanim system to achieve what I am looking for. Later on in the process I will be switching out animations which the mechanim system does not allow for.

    So the basics of what I am trying to develop. I am starting with 3 animations to keep things easy, the animations are intended to play in sequential order. The tricky part is I want to the player to not simple button mash to get a combo off, but actually have to click at the proper moment to trigger the next attack. So if the player starts in idle and I click, I start the first animation, if I click again while the animation is playing it will move to animation 2 when animation 1 is done. If the player clicks two or more times while animation 1 is playing, then only animation 2 will still play but not animation 3. The player still has to click during animation 2 to get to animation 3.

    How I am planning on doing this is setting up a simple switch that is true or false between animations. So in idle the click bool is set to false. Basically something like this:

    CLICK set bool to TRUE
    if bool is TRUE play animation1

    This part I know how to do, no issues there, its getting the clicks to coincide with the frames of the animation that I need to figure out. Right now each animation is set to 50 frames. I think it will work best if at the start of each new animation the CLICK bool is set to false, and only if the CLICK bool is set to true will the next animation in the sequence play. It seems like the easiest and most logical way to me.


    CLICK set bool to TRUE
    if bool is TRUE play animation1
    when animation1 starts set CLICK bool to false

    if CLICK bool is false at the end of animation1 return to idle animation
    if CLICK bool is true at the end of animation1 play animation2

    so on and so forth. It seems like a simple way to do it, but my problem is I do not know how to code this. I know how to play animations and get input from buttons and keys, but I don't know how to show the length of each animation in code or how to check if an animation i still playing or not. Any help at all from anyone would be great, thanks!
     
    Last edited: Feb 9, 2016
  2. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    If youre using mecanim, you can trigger animations just by changing a simple bool, int or float in the Animator. Play around with that ;)
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    True, sorry I should have added that I don't want to use the mechanim system. Mechanim does not allow animations to be swapped out, which is what I eventually will be doing with this system. So unfortunately I have to code things the hard way.
     
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Code (csharp):
    1.  
    2. public int animationCounter = 1;
    3.  
    4.  
    5.     void Update()
    6.     {
    7.         if (Input.GetButtonDown("Fire1"))
    8.         {
    9.             if (animationCounter == 1)
    10.             {
    11.                 GetComponent<Animation>().Play("SwingDown");
    12.                 animationCounter += 1;
    13.             }
    14.             else if (animationCounter == 2)
    15.             {
    16.                 GetComponent<Animation>().Play("SwingSide");
    17.                 animationCounter += 1;
    18.             }
    19.             else if (animationCounter == 3)
    20.             {
    21.                 GetComponent<Animation>().Play("SwingStab");
    22.             }
    Removed the clickAction. Isn't needed at all really, at least not right now. I may have to put it back in later.

    So now things play correctly, but if I click quickly I will cycle to the proper animation, stop the current animation, and play the selected animation. I need to figure out to stop the animations from overlapping. Any ideas?
     
    Last edited: Feb 9, 2016
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Code (csharp):
    1.  
    2. public int animationCounter = 1;
    3.     public bool clickAction = false;
    4.  
    5.  
    6.     void Update()
    7.     {
    8.         if (Input.GetButtonDown("Fire1"))
    9.         {
    10.             clickAction = true;
    11.         }
    12.  
    13.         if (clickAction == true)
    14.         {
    15.             if (animationCounter == 1)
    16.             {
    17.                 GetComponent<Animation>().Play("SwingDown");
    18.                 animationCounter += 1;
    19.                 clickAction = false;
    20.             }
    21.             else if (animationCounter == 2)
    22.             {
    23.                 GetComponent<Animation>().Play("SwingSide");
    24.                 animationCounter += 1;
    25.                 clickAction = false;
    26.             }
    27.             else if (animationCounter == 3)
    28.             {
    29.                 GetComponent<Animation>().Play("SwingStab");
    30.                 clickAction = false;
    31.             }
    Alright so I do have to add the clickAction back in. I know later it will be important because the player has to click while the animation is playing to get to the next animation, rather than the number just cycling up the animation number. Still trying to solve the problem of the animations, but I'll get on that.
     
  6. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Changing the Play to CrossFade sort of made it work, but not quite what I want. The animations play correctly when I click, but it plays an animation for each time I have clicked, regardless of what is happening with the current animation. So that won't work.
     
  7. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    Drop an Animator in that and try the New mecanim animation system.
    Its great!
    you can do like Animator.setFloat("test",1);
    Or Animator.getFloat("test") and in the unity editor you can add logic to automagically switch Animation according to float value.
    So you can get the float and use it to read/write the current animation state.
     
    Last edited: Feb 9, 2016
  8. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Can't use mechanim because what I want to do later with this system mechanim can't do.

    wow I have no idea what any of that is haha.
    so the Animator.setFloat, will that get the length of the animation to use later?
    What will the Animationsfilm do?

    Not sure I follow, sorry.
     
  9. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    Corrected my autocorrect typo, sorry :D
    Animators are great because they can automatically switch animations according to different given parameters. But if you cant use mecanim i think you cannot use the Animator. But lookup, probably i'm wrong.
    http://docs.unity3d.com/Manual/Animator.html

    Maybe you should try counting mouse clicks while the animation is playing.


    Different scripting improvement idea:

    1. public int animationCounter = 1;
    2. public bool clicked = false;
    3. public bool clickAction = false;


    4. void Update()
    5. {
    6. if (Input.GetButtonDown("Fire1"))
    7. {
    8. clickAction = true;
    9. }

    10. if (clickAction == true)
    11. {
    12. if (animationCounter == 1)
    13. {
    14. GetComponent<Animation>().Play("SwingDown");
    15. if (clicked == false) {
    16. animationCounter += 1;
    17. }
    18. else
    19. { //user clicked second/multiple time, do something fancy
    20. }
    1. clickAction = false;
    2. clicked = true;
    3. }
    4. }
     
    Last edited: Feb 9, 2016
  10. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Haha its alright. I'll look up how to use the animator better though. I have one attached but I only attached it to list the animations.

    Yeah its annoying that mechanim won't work. Later down the road animations will be switched out, and mechanim won't allow you to switch out animations in a sequence.

    Counting clicks might work, but if someone is button mashing then they can get around it, which is what I don't want. Makes the system more difficult to create for sure, but in the end requires the played to not just button mash to play.
     
  11. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hm tried it that way but it didn't seem to do much. the system I have now is perfect except that the animations overlap, if I can solve that I should be able to get the whole thing solved.
     
  12. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Looking through the manual it looks like AnimationState might hold my answer. I'll do some research