• Runtimes
  • [Unity]Turning colliders on/off

I want to be able to add my bounding boxes to spine, have them be converted to a 2d collider in Unity, attach these to the bone, and then be able to disable/enable the collider at certain times in the animation. A sword swinging for example would have a collider that is only active for a certain duration at the apex of the swing.

Is this currently possible with the unity runtime? Thanks!

Related Discussions
...

Not really.

The Bounding Boxes data are provided through the BoundingBoxAttachment class, but they are not currently enabled to do anything.

You are free to implement them however you wish as the data is exposed in Unity once a Skeleton is initialized. My personal opinion is that Unity 2D Physics are so flawed right now that they are just simply not worth the time.

If I were to implement what you want to do, I would iterate through all of the attachments on a skeleton when it first initializes and then generate polygon colliders for each attachment and disable it until its detected as the "Active" attachment then update its vertices and re-enable it. Ultimately though, unless it gets implemented similarly to SkeletonUtility (No Flip X for colliders, No Rotate Y180 for 2D colliders) then you end up with Physics Rigidbody update and recalculation every single frame of your animation... it makes collision detection very, very buggy at best.

Finally, to way I would implement something like a sword in Unity would be to have one or more raycasts that cast from the Hilt (you can put a Spine Bone Follower or use Skeleton Utility to find the hilt of the sword easily) when a Spine Event fires then detect what it hits and apply whatever physics effect or damage effect you want to anything in its path. I've often seen 2 events fired like "AttackStart" and "AttackFinish" where any frame in between is raycasted against to determine any potential targets.

Hope that helps some 🙂

"... then update its vertices and re-enable it". If I use a BoneComponent and just attach a collider to the animation, the collider moves with the bone automatically, as you showed in your video with the ragdoll goblin, correct? If I understand correctly, you are saying unless I use these limitations((No Flip X for colliders, No Rotate Y180 for 2D colliders), then it will be calculated extremely inefficiently?I am curious as to why this is, and where I could read up more on it, as I am a programmer and would love to possibly add an extension that would allow for something like this.

I am coming from SmoothMoves where this ability exists already, and I would love to do a kind of dismemberment system where one would have colliders on bones that move with the animation, and could send events when they are struck so that a dismemberment animation could occur, and even add physics to the dismembered limb. I am curious how SmoothMoves can do it seemingly efficiently.

Or if I wanted the player to take more damage if they were hit in the head vs. the leg. Having colliders on these limbs would be extremely important for a feature like that.

Thanks for all the advice!

I came to Spine from Smooves too 🙂

Dismembered limbs are actually possible as long as you dont have any HingeJoints when dealing with 2D Rigidbodies in Unity - so a single jointed dismembered limb is fine.

The efficiency issue comes in that BoundingBoxAttachments in spine would have their vertices calculated and built every frame to be re-aligned with the Skeleton object's current state. Smooves is built on top of the Unity Hierarchy, where as Spine has its own bone system so Smooves can simply parent colliders and Spine cannot because the hierarchy is calculated differently. Any time you apply Scale of any negative value to a 2D collider in Unity it tends to become unstable so the only way to "Flip" a collider without having a copy of the collider but inverted is to Rotate 180 (generally about the Y axis) which completely breaks 2D colliders... but not 3D colliders in Unity.

Sigh. I want a better solution to this too >.>

But if I use the skeleton utility to manually add a collider to a bone that has a BoneComponent, there aren't these same speed issues, correct?

mathius777 wrote

But if I use the skeleton utility to manually add a collider to a bone that has a BoneComponent, there aren't these same speed issues, correct?

It will have fewer speed issues if you use component based colliders, yes. Moving 3D colliders (especially without a Rigidbody) driven by animation (as opposed to Physics.AddForce or what not) in general is extremely expensive and buggy in Unity. I honestly don't know what the cost or quirks are for 2D colliders - it might be negligible.

Thanks for the info. Why would one use 3d colliders for a 2d animation? I wasn't going to have 500 characters on the screen anyways, so if the 2d collider cost is negligible that'd be great. Don't you do something similar with your ragdoll script? The video seemed to automatically add some 2d box colliders to the bones.

mathius777 wrote

Thanks for the info. Why would one use 3d colliders for a 2d animation? I wasn't going to have 500 characters on the screen anyways, so if the 2d collider cost is negligible that'd be great. Don't you do something similar with your ragdoll script? The video seemed to automatically add some 2d box colliders to the bones.

Yea, I revised Skeleton Utility to only use 3D stuff because there was no practical way to flip the character the other way with 2D colliders lol. Notice that original demo never flips >.>