Attaching meshes to bones
Attaching a mesh to a bone is exactly what it sounds like. Each time the animation updates the skeleton, the attached mesh is repositioned to stay in sync with the bone it is attached to. The sandbox provides an Animation.AttachToBone
function that attaches a mesh to a bone and allows for a position and rotation offset from the bone.
Tip
Once a mesh has been attached to the bone, you should discard the variable to the mesh, as it is no longer valid. It is best to assign the variable to nil in order to avoid errors.
Attaching a weapon to our soldier
Attaching a weapon to our soldier is very easy with the Animation.AttachToBone
function. Given the correct offset values, we can position a sniper rifle grip into the soldier's hand:
Sandbox.lua
:
function Sandbox_Initialize(sandbox) ... local weapon = Core.CreateMesh( sandbox, "models/futuristic_soldier/" .. "soldier_weapon.mesh"); Animation.AttachToBone( ...