Kurtav

Parry Animation (Spine+Unity)

Jun 30th, 2021 (edited)
1,557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. // An example of working with animation frames in Spine + Unity.
  2. // The function processes the animation of the attack of the knight and the zombie in such a way that their arms always intersect in the required certain frames. Parry simulation.
  3. https://www.youtube.com/watch?v=3z_9JAzvZ1U
  4. private IEnumerator HitParryInHandZombie()
  5. {// Before this call, the zombie had already started its attack animation.
  6.     skeleton.state.SetAnimation(0, attack, false); // skeleton - SkeletonAnimation Knight
  7.     skeleton.timeScale = 1;
  8.     TrackEntry teK = skeleton.state.GetCurrent(0);
  9.     float saveOldAnimEndK = teK.AnimationEnd;
  10.     teK.AnimationEnd = 0.54f;
  11.     float timeFrameAttackInHande = 0.54f;
  12.     TrackEntry teZ = AnimZombieSpine.In.GetTrackEntry();
  13.     float saveOldAnimEndZ = teZ.AnimationEnd;
  14.     teZ.AnimationEnd = 0.44f;
  15.     float resultFrame = 0.44f  - teZ.AnimationTime; // how many frames are left before the keyframe for parry preparation
  16.     AnimZombieSpine.In.SetTimeScale(resultFrame / timeFrameAttackInHande); // = The formula for changing the attack speed of a zombie        
  17.     // If the zombie has a lot of frames left before the key frame of preparing the parry(teZ.AnimationEnd = 0.44f), he will move his hand faster. If the zombie has few frames left, he will move his hand more slowly
  18.     //The formula for changing the attack speed of a zombie, the delay of coroutine , of course, does not calculate everything perfectly. There are inaccuracies in timing and animation frames. The most important thing about this function is fixing any inaccuracies with the following lines of code: teZ.AnimationEnd = 0.44f; teK.AnimationEnd = 0.54f; teK.TrackTime = 0.54f; teZ.TrackTime = 0.44f;
  19.     yield return new WaitForSeconds(timeFrameAttackInHande);
  20.     // method explanation -
  21.     // This is the preparation of the knight and zombies for the parry animation. They put their hands in specific positions for a specific frame of the animation. In order for the swing animation to be perfectly synchronized frame by frame and the coroutine delay does not desync anything, you need to simulate the end of the animation much earlier. If someone arrives at the desired animation frame before the set coroutine time delay, they will simply stop at the simulation end frame. It's just that one of the two will freeze for the shortest time period not noticeable to the eye.
  22.     teK.TrackTime = 0.54f; // this is 100% anchoring of frame positions before parrying
  23.     teZ.TrackTime = 0.44f; // this is 100% anchoring of frame positions before parrying
  24.     AnimZombieSpine.In.SetTimeScale(0.7f);
  25.     Time.timeScale = 0.3f;
  26.     teK.AnimationEnd = saveOldAnimEndK;
  27.     teK.TimeScale = 0.05f;
  28.     teZ.AnimationEnd = saveOldAnimEndZ;
  29.     teZ.TimeScale = 0.3f;
  30.     hitWhite.Play(); // particles
  31.  
  32.     yield return new WaitForSeconds(0.1f);
  33.  
  34.     ControlSwords.In.HitAttackSword();
  35.     Time.timeScale = 1f;
  36.  
  37.     yield return new WaitForSeconds(0.1f);  
  38.  
  39.     teK.TimeScale = 1;
  40.     teZ.TimeScale = 1;
  41. }
Add Comment
Please, Sign In to add comment