Attach item to bone
Code to attach a sword, weapon, etc. to one specific bone, e.g. the hands.
--------------------------
--Code for ShiVa >= 1.9.1:
--------------------------
--inside the onInit() of the character we want to bind to
local hDummy = scene.createRuntimeObject ( application.getCurrentUserScene ( ), "Forearm_Armor" )
object.setParent ( hDummy, this.getObject ( ), false )
if object.bindTransformToParentSkeletonJoint ( hDummy, "RForearm" ) then
log.message ( "skeleton binding successful!" )
end
-------------------------
--Code for ShiVa < 1.9.1:
-------------------------
--onInit()
function AICharacter.onInit ( )
-- Some code--
-- Create the sword--
local sword = scene.createRuntimeObject ( object.getScene ( this.getObject ( ) ), "MyBeautifulSword" )
if ( sword )
then
-- Some sword setup code--
-- Take a handle on the created sword--
this.hSword ( sword )
end
end
----------------
--onEnterFrame()
----------------
function AICharacter.onEnterFrame ( )
-- Some code--
-- Place the sword at the right position/orientation--
local o = this.getObject ( )
local sword = this.hSword ( )
local jointPosX, jointPosY, jointPosZ = shape.getSkeletonJointTranslation ( o, "RightHandJoint", object.kGlobalSpace )
local jointRotX, jointRotY, jointRotZ = shape.getSkeletonJointRotation ( o, "RightHandJoint", object.kGlobalSpace )
object.setTranslation ( sword, jointPosX, jointPosY, jointPosZ, object.kGlobalSpace )
object.setRotation ( sword, jointRotX, jointRotY, jointRotZ, object.kGlobalSpace )
end