Rigging in Blender for Unity Mecanim

 Mecanim, Unity 3D  Comments Off on Rigging in Blender for Unity Mecanim
May 132013
 

As a programmer I’m not too well versed in all things graphics, but sometimes Blender comes in handy. After Unity introduced Mecanim, setting up animations isn’t all that complicated anymore and as long as you have a character with a mesh you can do the rigging yourself.

So here’s one way of rigging a character in Blender for use with Mecanim, as efficient and no-fuss as possible, with an already existing mesh; since I’m not skilled enough to make the mesh myself.

NOTE! This is meant to be a short efficient tutorial on how to rig a character by using the Meta-Rig that comes with the Rigify add-on. If you need more advanced use of Blender, I recommend you to try the Blender forums or just plain google it. That’s what I did 😉

Okay so here goes!

Activate Rigify add-on

To activate the Rigify add-on which includes the Human(Meta-Rig), goto File > User Preferences > Add-ons > Search for “Rigify” > Tick the little box on the right of Rigify.

Add the Human(Meta-Rig) to the scene

Select your Mesh, goto Add > Armature > Human(Meta-Rig) and voila! there it is.

Scale

Now, the Meta-Rig might be scaled wrong for your character, so let’s rescale it. Select the meta-rig, hit S(to activate scaling) and scale the rig to fit your mesh. Don’t worry about making it perfect for now, as long as it’s about the correct size. One important thing to remember here is that you need to reset the scale and rotation for the rig, so your current scale/rotation is represented with 1,1,1 and 0,0,0. So go to Object Mode, make sure the rig is selected and hit Ctrl+A and choose Rotation & Scale in the menu that pops up.

Aligning the rig

Now’s the time to go over the rig and align all bones to fit with your mesh. This is important and sometimes time-consuming work, especially the fingers, but if you keep in mind a few shortcuts you’ll be fine:
Focus on selected object: Numpad ,
Move object freely: G
Rectangle-select objects: B
Select all: A

Connect the rig to the mesh with weights

After you’ve aligned the rig and everything is peachy, it’s time to connect the rig to the mesh. Since I’m a programmer I prefer connecting with automatic weighting, but there’s a couple preparations that make the process easier. First, select your mesh, goto Object modifiers(the wrench icon) below the Scene tree and Add modifier > Armature; make sure Bind To has Vertex Groups checked and Bone Envelopes unchecked; and that object lists your rig and that Preserve Volume is checked. Don’t hit apply or anything.
Select your rig, goto Pose mode, select your mesh and goto Weight mode. Hit A to make sure everything is selected and then goto menu Weights > Assign Automatic From Bones.

Troubleshooting

Sometimes this fails, and there’s isn’t any good reason why. Usually it’s because some of your rig is aligned wrong or the rotation and scale might not be reset and so on. So I will list a few tricks I have found works when this happens:

  • Align your rig: Try to ensure that all your bones are aligned inside the mesh. I’ve found that the fingers usually are the problem here, but not always.
  • Reset rotation & scale: In Object Mode, hit Ctrl+A and choose Rotation & Scale. Do it both on the mesh and rig.
  • Remove doubles: With your mesh selected under Edit Mode, hit W and select Remove Doubles. This can also be found under the Mesh menu.
  • Recalculate normals: With mesh selected and in Edit Mode, hit Ctrl+N to recalculate all normals. This can also be found under the Mesh menu.

Unity Mecanim

Once the rig is done, save it your Unity asset folder as .blend, open Unity, let it import itself, select the file and under the Rig tab in the Inspector make sure Animation Type is Humanoid(if the character is a humanoid) and Avatar Definition is “Create From This Model”. Hit apply and Unity re-imports your character and automatically checks to see if the rig is Mecanim compatible. Once re-import is done, hit Configure and you will get up the Mecanim bone mapping GUI. If everything went smoothly, your bones should already be mapped here, if not you can try apply Mapping > Automap in the lower left corner, or assign the bones manually.

That’s it!

If you need more information on Mecanim, I strongly suggest looking up the official tutorial or as before, google it! 🙂

SphereCast and Layermasks

 Scripting, Unity 3D  Comments Off on SphereCast and Layermasks
Sep 032012
 

I’ve been struggling with raycasting and making everything between the camera and a target transparent.

I found a lovely script over at unityAnswers that worked just perfect!

But I found that a regular raycast was way too thin for my purposes, so I decided to check out Unity’s SphereCastAll class.
Now, googling for any information about SphereCasting is, how should I put it? Confusing!
Thanks to this excellent bug report by The Ant Ranch I finally got confirmed that this is exactly what I was looking for.

But still I couldn’t get it to work properly… It lagged like hell and the hits were incoherent. That’s before I decided to find out what the hell Layermasks were all about.
Thanks to unityAnswers again, I found this excellent answer on the topic of Layermasks, which was the final drop that I needed to make this work as I wanted it to.

Hope this research will be of use to someone else too 🙂