• RuntimesUnity
  • Can I use Spine animation as a damage timer?

I created an enemy. The enemy is simple and does not have a rigid body (which optimizes performance).
I'm moving the enemy using transform.position in the Update method.

The enemy can attack the hero. The attack animation also runs in the Update method.

On frame 90 of the attack animation, the enemy must deal damage to the hero. Here is the code for getting the attack frame and dealing damage to the hero.

currentAnimationFrameNumber = Mathf.FloorToInt(currentTrackEntry.AnimationTime * 60);

if (currentAnimationFrameNumber==90) EnemyCharacterDealDamage();

Can I deal damage to a hero based on the attack frame?

Does the code above guarantee that the hit will be dealt 1.5 seconds later?
Adjusted for frame rounding, for example at 1.52 or 1.48 seconds?

Spine animation is guaranteed to be executed in the code for exactly the specified time, regardless of the video adapter frequency, FPS drops (if the 90th frame was not displayed on the screen, but will it be processed in the attack code?)

Related Discussions
...

Yes, you can use Spine animation as a damage timer. However, there are a few considerations to ensure that the damage is dealt accurately at the specified time, regardless of frame rate variations or FPS drops.

Spine animations are time-based rather than frame-based. This means that the animation's progress is calculated using elapsed time, which ensures smooth playback even if the frame rate fluctuates. Here’s how you can ensure that the damage is dealt accurately:

  1. Use Time-Based Calculation: Instead of relying on frame numbers, use the time in seconds to determine when to deal damage. Spine animations are typically set to 30 frames per second, so frame 90 corresponds to 3 seconds (90 / 30).

  2. Event Handling: Spine allows you to set up events within the animation timeline. You can create an event at the 3-second mark (or frame 90) in the Spine editor and handle this event in your code to deal damage.

Here’s an example of how you can set up and handle an event in Spine:

Setting Up an Event in Spine Editor

  1. Open your attack animation in the Spine editor.
  2. Go to the frame where you want the event to occur (frame 90).
  3. Add an event at this frame and name it, for example, "dealDamage".

Handling the Event in Code

In your code, you can listen for this event and deal damage when it occurs:

void Start() {
    skeletonAnimation.AnimationState.Event += HandleEvent;
}

void HandleEvent(Spine.TrackEntry trackEntry, Spine.Event e) {
    if (e.Data.Name == "dealDamage") {
        EnemyCharacterDealDamage();
    }
}

Ensuring Accurate Timing

If you prefer to stick with your current approach, you can adjust it to use time-based calculations:

float attackTime = 3.0f; // 3 seconds for frame 90 at 30 fps

if (currentTrackEntry.AnimationTime >= attackTime && !damageDealt) {
    EnemyCharacterDealDamage();
    damageDealt = true; // Ensure damage is dealt only once
}

This approach ensures that the damage is dealt at the correct time, regardless of frame rate variations.

Summary

Yes, you can use Spine animation as a damage timer. Using time-based calculations or Spine events ensures that the damage is dealt accurately at the specified time, regardless of frame rate variations or FPS drops.