Topic: Tutorial: Hitbox Auto-Setup Button for any rig

Initially, I was just going to post the code for Blender Rigify rigs, but since this addition was actually quite easy, I thought it was a good idea to make a tutorial so others can implement it for their own skeleton systems.  This should work for any rig created from any package, you just need to know the names of your bones.

This will work in all versions as I think the CharacterEditorWindow.cs script is included in all versions.

Firstly, in Unity/UFE, manually create the hitboxes for one character that has the rig you want to use.  Make sure you assign hitboxes the correct bone types (deformation bones typically in a heirachy, not IK bones).  If you already have a character ready with this requirement, then you can use it.

Open the character in UFE's Character Editor, expand Hit Box Setup and press Open Character.  Expand the Hit Boxes, we'll need to refer back to it soon.

Then in your text editor, open CharacterEditorWindow.cs, and find this chunk of code:

                                    EditorGUI.BeginDisabledGroup(FindTransform("Head") == null);{
                                        if (StyledButton("Auto-Setup for Mixamo Auto-rigger")){
                                            if (EditorUtility.DisplayDialog("Replace Hitboxes?", 
                                                                            "This action will delete your current hitbox setup. Are you sure you want to replace it?",
                                                                            "Yes", "No")){
                                                hitBoxesScript.hitBoxes = new HitBox[0];
                                                for(int i = 0; i <= 14; i ++){
                                                    HitBox newHitBox = new HitBox();
                                                    if (i == 0){
                                                        newHitBox.bodyPart = BodyPart.head;
                                                        newHitBox.collisionType = CollisionType.bodyCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .7f;
                                                        newHitBox.position = FindTransform("Head");
                                                    /*}else if (i == 1){
                                                        newHitBox.bodyPart = BodyPart.neck;
                                                        newHitBox.collisionType = CollisionType.noCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .2f;
                                                        newHitBox.position = FindTransform("Neck");*/
                                                    }else if (i == 1){
                                                        newHitBox.bodyPart = BodyPart.upperTorso;
                                                        newHitBox.collisionType = CollisionType.bodyCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .9f;
                                                        newHitBox.position = FindTransform("Spine2");
                                                    }else if (i == 2){
                                                        newHitBox.bodyPart = BodyPart.lowerTorso;
                                                        newHitBox.collisionType = CollisionType.bodyCollider;
                                                        newHitBox.type = HitBoxType.low;
                                                        newHitBox.radius = .9f;
                                                        newHitBox.position = FindTransform("Spine");
                                                    }else if (i == 3){
                                                        newHitBox.bodyPart = BodyPart.leftUpperArm;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("LeftArm");
                                                    }else if (i == 4){
                                                        newHitBox.bodyPart = BodyPart.rightUpperArm;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("RightArm");
                                                    }else if (i == 5){
                                                        newHitBox.bodyPart = BodyPart.leftForearm;
                                                        newHitBox.collisionType = CollisionType.noCollider;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("LeftForeArm");
                                                    }else if (i == 6){
                                                        newHitBox.bodyPart = BodyPart.rightForearm;
                                                        newHitBox.collisionType = CollisionType.noCollider;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("RightForeArm");
                                                    }else if (i == 7){
                                                        newHitBox.bodyPart = BodyPart.leftHand;
                                                        newHitBox.collisionType = CollisionType.noCollider;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("LeftHand");
                                                    }else if (i == 8){
                                                        newHitBox.bodyPart = BodyPart.rightHand;
                                                        newHitBox.collisionType = CollisionType.noCollider;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("RightHand");
                                                    }else if (i == 9){
                                                        newHitBox.bodyPart = BodyPart.leftThigh;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.low;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("LeftUpLeg");
                                                    }else if (i == 10){
                                                        newHitBox.bodyPart = BodyPart.rightThigh;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.low;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("RightUpLeg");
                                                    }else if (i == 11){
                                                        newHitBox.bodyPart = BodyPart.leftCalf;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.low;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("LeftLeg");
                                                    }else if (i == 12){
                                                        newHitBox.bodyPart = BodyPart.rightCalf;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.low;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("RightLeg");
                                                    }else if (i == 13){
                                                        newHitBox.bodyPart = BodyPart.leftFoot;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.low;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("LeftFoot");
                                                    }else if (i == 14){
                                                        newHitBox.bodyPart = BodyPart.rightFoot;
                                                        newHitBox.collisionType = CollisionType.hitCollider;
                                                        newHitBox.type = HitBoxType.low;
                                                        newHitBox.radius = .5f;
                                                        newHitBox.position = FindTransform("RightFoot");
                                                    }
                                                    hitBoxesScript.hitBoxes = AddElement<HitBox>(hitBoxesScript.hitBoxes, newHitBox);
                                                }
                                            }
                                        }
                                    }EditorGUI.EndDisabledGroup();

and copy it and paste it just below.  You've just created a second button that does the same thing as Mixamo Auto-Rig Setup.  We're going to edit this copy.  I recommend adding a comment just before your pasted statement so you know which button is which.

From here on, you'll be editing the pasted block of text, NOT the original you copied from.  MAKE SURE YOU ARE WORKING ON A COPY OF THE TEXT, otherwise you'll lose the Mixamo AutoRig button!


Where is says:

if (StyledButton("Auto-Setup for Mixamo Auto-rigger")){

change the button name to suit.  I used "Auto setup for Blender Rigify".


The Mixamo auto rigger button is inactive if a certain bone is missing.  You can do the same by looking for a bone that's specific to your rig.  In the line just above the button name, you'll see:

EditorGUI.BeginDisabledGroup(FindTransform("Head") == null);{

Change it from Head to something specific to your rig.  In my case with Blender Rigify, I went with ORG-hips.


Then just a few lines down, you'll see a for loop with this chunk of code near the top:

                                                    if (i == 0){
                                                        newHitBox.bodyPart = BodyPart.head;
                                                        newHitBox.collisionType = CollisionType.bodyCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .7f;
                                                        newHitBox.position = FindTransform("Head");

The Mixamo AutoRig setup simply looks for named transforms and assigns hitboxes appropriately.  All you need to do here is in each if statement, change the transform you're looking for to the appropriate one in your rig.  Here's what the above one looks like in mine:

                                                    if (i == 0){
                                                        newHitBox.bodyPart = BodyPart.head;
                                                        newHitBox.collisionType = CollisionType.bodyCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .7f;
                                                        newHitBox.position = FindTransform("ORG-head");

Note that with Blender Rigify, for the upperTorso hitbox I had to offset the chest bone since it was too low.  See the below snippet for how that's handled.  Note that the offset has to be after the transform search, else it won't work.

                                                    }else if (i == 1){
                                                        newHitBox.bodyPart = BodyPart.upperTorso;
                                                        newHitBox.collisionType = CollisionType.bodyCollider;
                                                        newHitBox.type = HitBoxType.high;
                                                        newHitBox.radius = .9f;
                                                        newHitBox.position = FindTransform("ORG-chest");
                                                        newHitBox.offSet = new Vector2(0f, .5f);

Once you have done all hitboxes, save the script.  Then in UFE open a new character with the same rig.  You should see the Mixamo button disabled but your new one is active.  Test the button and you should find the new button auto set ups the hitboxes.  If there are missing hitboxes, check the spelling of the transform names you entered.

If you want to take this further, try creating buttons that do specific set ups.  Like auto set up with weapons, if you were making a soul calibur type fighter; or auto set up with wings/tails if your game has some characters with those components.  There are 9 custom bones that could easily be used for such things.

Share

Thumbs up +4 Thumbs down

Re: Tutorial: Hitbox Auto-Setup Button for any rig

What's up Yumcha you are a life saver bro! Thanks so much because I never knew that we could create our own auto-rig. Right after reading your guide I flawlessly added an auto rig for DAZ Genesis and 3dFoin characters! Thanks again Yumcha, Cheers!

Share

Thumbs up +1 Thumbs down

Re: Tutorial: Hitbox Auto-Setup Button for any rig

Fantastic stuff, a real timesaver for me smile

Thanks a lot man!

Share

Thumbs up Thumbs down

Re: Tutorial: Hitbox Auto-Setup Button for any rig

Thanks for your tutorial, it really saves a lot of time!

Share

Thumbs up Thumbs down

Re: Tutorial: Hitbox Auto-Setup Button for any rig

This is great! Thank you very much for sharing YumChaGames! smile

Share

Thumbs up Thumbs down