Search Unity

Doing a Beat'em Up, how to approach the attack

Discussion in 'Scripting' started by Xavy_clay, Oct 7, 2015.

  1. Xavy_clay

    Xavy_clay

    Joined:
    Oct 2, 2015
    Posts:
    20
    Hi there!

    (I introduced my project here)

    So, as the title says, I'm doing a Beat'em up ala Streets if Rage. The plan is, at some point, to have different attacks, a parry/dodge and a Grab (no button, just walk "into" an enemy).

    I've seen people that do it creating an empty object (Attack Point) that floats in front of the character and use it as a a casting point to look for colliders,
    Other people uses the whole boxcollider that represents the character to look for triggers; that would mean altering the collider in some animations (there are sprites that are larger than others, but could be worked around to make all sprite "equal").

    Also, I haven't decided yet if the game will be un full 2D (with "fake" 3D effect) or not, but at the moment I'm working with 2D, no physics.

    Rigth now I'm going with the first one, the casting point to test the simple attack mechanic. If someone around here has been there, I'd really apreciate the tips or advices. If there is a better (or easiest) way to do this, I'll be gratefull.

    See you around!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Seems like the old 2D brawlers just did simple distance checks.

    Something like this, pseudocode:
    Code (csharp):
    1.  
    2. if(currentAnimationFrame == attackFrame) // like the frame when our fist is fully extended)
    3. {
    4.     foreach(enemy)
    5.     {
    6.         if(enemy.position is in front of us and within range)
    7.         {
    8.             enemy.TakeDamage();
    9.         }
    10.     }
    11. }
    12.  
    13.  
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    Your approach sounds fine. Rather than making other suggestions, like adding animation curves that specify how much power an attack animation has at various keyframes, I recommend you scour Universal Fighting Engine's documentation. They've been fine-tuning this stuff for years, so you might as well give yourself a head start by learning from their experiences rather than having to discover them all over again. You don't have to do things they way they do, but you can borrow concepts and familiarize yourself with pitfalls they've had to overcome.
     
    wlwl2 likes this.
  4. Xavy_clay

    Xavy_clay

    Joined:
    Oct 2, 2015
    Posts:
    20
    I totally overlooked the UFE, but it makes sense to give it a look. I should be able to find some ideas about hitboxes, chain moves and linking animations.

    Right now I have a simple code that idles, walks and attacks. I have some issues with the whole transition between states:

    Idle has 2 frame animation and transitions to Walk with a boolean that catches kewboard input.

    Walk has 3 frame animation that plays as long as a boolean is on true, if not reverts to Idle. Should I make the input something like "onKeyDown" event? I'm coding on the Updtae function as I have no physics.

    I plan to use the Animator conditions (parameters, booleans, onTriggers, etc), but I fear that may "lag" the transitions. I'm in the right path?
     
  5. Xavy_clay

    Xavy_clay

    Joined:
    Oct 2, 2015
    Posts:
    20
    I've been tweaking a bit. Now the idle->walk->idle transition run smooth and cool, but another issue arises with the Attack animation.

    I have a trigger parameter that transitions from "any state" to "attack"; I catch the input with getButtonDown and set the trigger for the animation to star via code, then catch an event when animation ends to get back to Idle. It works, but it seems like the trigger for "start attack" sometimes does not work and makes the whole thing feel "laggy".

    Any advice?
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    While playing, click on your character and watch the live view of your animator controller in the Animator view. It might show you whether it's lagging when setting the trigger or when transitioning to attack.
     
  7. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    "Has Exit Time : Determines whether the transition’s condition can take effect at any time, or only during the state’s exit time."

    This is possibly what's making your transitions look weird. If HasExitTime is checked for the transition from any state to attack, it can't start the transition until the current state has reached a certain time in it's execution.

    see : http://docs.unity3d.com/Manual/class-Transition.html
     
  8. Xavy_clay

    Xavy_clay

    Joined:
    Oct 2, 2015
    Posts:
    20
    Thanks for the replies.

    I did what TonyLi suggested, and seems that the trigger used to go from "Any State" to "Attack" sometimes do not trigger.

    Code (CSharp):
    1. if (Input.GetButtonDown("Fire1") && !isJumping && !isAttacking)
    2.         {
    3.             isAttacking = true;
    4.             myAnimator.SetTrigger("attacked");
    5.         }
    isAttaking is my control flag for what can or can't do.
    "attacked" is a boolean trigger in the Animator Controller.

    This is located at the end of the Update() function. It's the code itself that is running slow?
     
    Last edited: Oct 8, 2015
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    isJump or isAttacking is probably true, which would prevent that code (SetTrigger, etc.) from running.

    This suggestion skirts around the problem instead of addressing it, but why not allow the player to set the "attacked" trigger at any time, even while still attacking or jumping? If you set the attack state atomic, it won't interrupt itself if the player mashes the attack button. This might make the controller feel more responsive, too.

    p.s. - If you end up posting longer blocks of code, please help out your readers by using code tags.
     
  10. Xavy_clay

    Xavy_clay

    Joined:
    Oct 2, 2015
    Posts:
    20
    The idea behind a flag for "isAttacking" is prevent the object form moving while attacking (the attack animation do not have translation); if you keep pressing forward while attacking, the sprite moves sliding while throwing a punch.

    p.s - sorry about the code part, solved; won't happen again :p


    EDITED: found one thing that makes the game run smooth. I increased the speed of the "idle" animation (actually not an animation yet) and put all transitions from state to state (not Any State anymore). Now Attack responds better.
     
    Last edited: Oct 8, 2015
  11. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    That sounds like a good solution since the transition arrows show you at a glance what states are allowed to transition to other states.