• Runtimes
  • [Unity] Random bone explosion

  • Изменено
Related Discussions
...

I wanted to create explosion of all bones in Spine/Unity. I could not find any information in this forum - only following topic without answer:
http://de.esotericsoftware.com/forum/viewtopic.php?f=7&t=2739

So I had to do it by myself. I have achieved:


The bones are moving along parabolic trajectory.

What you need to do to achieve similar effect:

  1. You have to create another SkeletonData with all bones need to have root as a parent!
    skeleton.json file:

    [...]
       { "name": "bone5", "parent": "root", "length": 182.32, "x": 97.44, "y": 640.42, "rotation": -151.65 },
       { "name": "bone6", "parent": "root", "length": 58.17, "x": -89.28, "y": 543.18, "rotation": -102.99 },
       { "name": "bone7", "parent": "root", "length": 68.1, "x": -71.46, "y": 826.5, "rotation": -131.52 },
    etc.
    [...]
  2. Add following code to your object with SkeletonAnimation component:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class BoneMovementData
    {
       public int id;
       public float startPointX;
       public float startPointY;
       public float angle;
       public float velocity;
       public float angleCos;
       public float angleTan;
       public float motion_b;
       public float rotation;
    
       private float gravity = 10F;
    
       public BoneMovementData(int boneId, float boneStartPointX, float boneStartPointY)
       {
          id = boneId;
          startPointX = boneStartPointX;
          startPointY = boneStartPointY;
          angle = Random.Range(0F,Mathf.PI); // Random Angle
          velocity = Random.Range(60F,100F); // Random Velocity
          angleCos = Mathf.Cos(angle);
          angleTan = Mathf.Tan(velocity);
          motion_b = -1 * gravity/(2 * Mathf.Pow(velocity, 2) * Mathf.Pow(Mathf.Cos(angle), 2));
          rotation = Random.Range(-10F,10F);
       }
    }
    
    public class RandomBoneMovement : MonoBehaviour {
    
       private SkeletonAnimation skeletonAnimation;
    
       private float t;
       private float curveX;
       private float curveY;
    
       List<BoneMovementData> boneMovementData = new List<BoneMovementData>();
    
       void ParabolicMovement(BoneMovementData bd)
       {
          curveX = bd.velocity * t * bd.angleCos;
          curveY = bd.angleTan * curveX + bd.motion_b * Mathf.Pow(curveX,2);
    
      skeletonAnimation.skeleton.bones[bd.id].x = bd.startPointX - curveX;
      skeletonAnimation.skeleton.bones[bd.id].y = bd.startPointY + curveY;
      skeletonAnimation.skeleton.bones[bd.id].rotation += bd.rotation;
       }
    
       void Start () 
       {
          skeletonAnimation = this.GetComponent<SkeletonAnimation>();
    
      for (int i = 1; i < skeletonAnimation.skeleton.bones.Count; i++)
      {
         boneMovementData.Add(new BoneMovementData(i, skeletonAnimation.skeleton.bones[i].x, skeletonAnimation.skeleton.bones[i].y));
      }
       }
    
       void Update () {
          t += Time.deltaTime * 20F; // Speed Parameter
    
      for (int i = 1; i < boneMovementData.Count; i++)
      {
         ParabolicMovement(boneMovementData[i]);
      }
       }
    }
    1. (Optional) I had to explode object after animation, so I have my SkeletonAnimation object with all animations and a child object (inactive) with all bones attached to root. When I trigger an explosion, I remove SkeletonAnimation and MeshRenderer from parent object and make child object active.

    That's it. Hope it will help somebody :-)

    BTW, I am developing cool game Lichtspeer using Spine/Unity if you have any feedback I will be glad to hear from you :-)
    http://www.lichtspeer.com

Nice.
Mitch developed a similar exploding feature (using SkeletonUtility) that also allows some groups of bones to stick together, and the whole skeleton to un-explode/ease back into setup pose.

This one looks more like it's done right inside the Spine system though. Nice alternative to know about.

PS
Fun game.
I keep (accidentally) spearing zombies in the crotch, like 80% of the time.

That game is hilarious and satisfying! There seems to be a bug though, where the spear you're charging up disappears some of the time


it seems to happen at the same time a spear you've already thrown misses and hits the ground or goes off screen. OH MY GOD IT'S ANNOYING.

6 дней спустя
alic написал

There seems to be a bug though, where the spear you're charging up disappears some of the time


it seems to happen at the same time a spear you've already thrown misses and hits the ground or goes off screen. OH MY GOD IT'S ANNOYING.

Thanks for your notice. Many people also pointed it out. This prototype is 2 months old. In next released version it will be changed :-) We will release next version in 1-2 weeks and I will put info in Showcase subforum in order not to make mess here.