Documentation/Programming Tutorials/Attach Camera to a Bone

From NeoAxis Engine Wiki

Jump to: navigation, search
Go to higher level

Have you ever wanted to have your camera move and wave along with your character, or make peeking around the corner with just an animation, or realistic recoiling from your oponents? Well here is a quick way to do it:

1. Open the Game project.

2. Find and open ActionGameWindow.cs

3. Find these lines:

else
{
    position = unit.GetInterpolatedPosition();
    position += unit.Type.FPSCameraOffset * unit.GetInterpolatedRotation();
}

4. Then replace them with:

else
{
    Vec3 pos = Vec3.Zero;
    Quat rot = Quat.Identity;
    Vec3 scl = Vec3.Zero;
    bool hasHead = false;
 
    foreach(MapObjectAttachedObject obj in unit.AttachedObjects)
    {
        if (obj as MapObjectAttachedHelper == null)
            continue;
 
        if (obj.Alias == "Head")
        {
            obj.GetGlobalInterpolatedTransform(out pos, out rot, out scl);
            hasHead = true;
            break;
        }
    }
 
    if (hasHead)
    {
        position = pos;
        position += unit.Type.FPSCameraOffset * rot;
    }
    else
    {
        position = unit.GetInterpolatedPosition();
        position += unit.Type.FPSCameraOffset * unit.GetInterpolatedRotation();
    }
}

5. Open your Resource Editor and then Rabbit.type (or any of your PlayerCharacter).

6. Add the head bone slot in the mesh:

AttachCameraToBone 01.jpg

7. Attach helper object:

AttachCameraToBone 02.jpg

8. Set its scale to 0.1 0.1 0.1 (irrevelant but visualy helps), its alias to Head (this is needed for recognizing), and its bone slot to head (also very important):

AttachCameraToBone 03.jpg

9. Save, run any level and enjoy!

P.S. Rabbits animation speed (and animation) is very weird (in positive way) as you will see directly from his eyes :)


Original post is here: http://www.neoaxisgroup.com/phpBB2/viewtopic.php?t=2873