Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Frame Advantage and Disadvantage on Attacks for a 2D Fighting Game or Beat Em Up

Discussion in '2D' started by beserkgorilla, Dec 30, 2020.

  1. beserkgorilla

    beserkgorilla

    Joined:
    Jan 19, 2018
    Posts:
    2
    Hello! I'm relatively new to Unity and working on my first from-scratch project test game.

    I have an animated player character with attacks and hitboxes that spawn during their attack animation to check for collisions, but now I'm trying to figure out how to gauge and measure the animation lengths so that I can create consistent frame advantage and disadvantage for the player character depending on whether the attack hit or was blocked.

    To drop the fighting game lingo, I'm hoping to find a way to get a consistent measure of exactly how long it takes for the first hurtbox of my character's attack animation to appear from their idle animation. I'm hoping to find out how long the animation needs to loop so on hit the player character can continue attacking while the enemy is still in hit stun (creating a combo) and on block the player character will recover later than the enemy recovers, making them vulnerable to counter attack. I'll need to find a way to get a fairly precise measure of how long the startup and recovery of my attack animations are so that I can implement situations where a fast attack can combo but a slow one cannot, or alternatively a fast attack can punish a blocked attack but a slower one cannot.

    My first idea was to try and lock the game to 60FPS and then use a script to update an int frameCount variable (in the update() function) so I can measure exactly how many frames it takes for my attack to come out. Unfortunately, I'm not getting a consistent number this way (the number will typically vary about +-4~5 frames even though I'm repeating the same attack) so I assume there is something about the Update function that won't let it work like this.

    My next idea is to find the length of an animation clip using AnimationClip.length, but I'm still experimenting with the implementation of this. While I continue working on this, I was hoping one of you on the forums may have some experience with implementing a mechanic like this or an idea on how to approach it effectively.

    Thank you in advance for any ideas you may have and thank you for reading my long post!
     
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    c# has a class called stopwatch that you can use to measure time between events
     
  3. beserkgorilla

    beserkgorilla

    Joined:
    Jan 19, 2018
    Posts:
    2
    Thank you for the suggestion! I'll give this a shot and hopefully I'll be able to segment the attack animation into separate phases (startup, active, and recovery) using animation events and then time them with stopwatch.