This code example shows how to make the avatar look at a particular object. All the code and assets are in the projects Scripts directory. You can download the project using the link above.
// Turn on debug so that Chat.printDebug() and Chat.dump() work
Chat.debugOutput = true;
// Display the stats panel in the bottom left corner of screen
Application.statsVisible = true;
// Turn on collisions, but allow the user to turn it off
Actor.setCollisionSetting( true, false );
// Turn on gravity, but allow the user to turn it off
Actor.setGravitySetting( true, false );
// Show the avatar
Actor.showAvatar = true;
// Show the chat panel
Application.chatPaneVisible = true;
// Set ambient light for the scene
Scene.ambientLightColor = Color(0.5, 0.5, 0.5, 0);
// Create the floor
var oGround = Scene.createGround( "MainFloor", "RustySteel", 3000.0, 3000.0, true );
// Create a Skydome
oSkyDome = Scene.createSkyDome();
oSkyDome.materialName = "CloudySky";
oSkyDome.curvature = 5;
oSkyDome.tiling = 8;
oSkyDome.distance = 4000;
oSkyDome.enable = true;
// Create a point light
oPointLight = Scene.createPointLight( "SceneLight" );
oPointLight.position = Vector( 20, 300, 50 );
// Determine if the avatar has a Stealth animation
if ( Actor.hasAnimation( "Stealth" ) )
Chat.print( "Stealth animation available" );
// Determine if the avatar has a Crouch animation
if ( Actor.hasAnimation( "Crouch" ) )
Chat.print( "Crouch animation available" );
// Print the names of all animations in the avatar
Chat.print( "Names: ", Actor.getAnimationStateNames() );
// Set up a keyboard event listener.
this.oKeyListener = new Object();
this.oKeyListener.onKeyDown = function()
{
if ( Key.isKeyDown( Key.KEY_M ) )
{
Chat.print( "Setting Movement Animation" );
// As a test, give the avatar a much larger collision capsule, the vector specifies the size
// x = radius, Y = height, z is unused for capsules
// Actor.setMovementAnimation( "Stealth", true, 0, Vector( 100, 250, 0) );
// Crouch and stay crouched even when not moving
Actor.setMovementAnimation( "Crouch", true, 0, Vector( 0, 0, 0) );
}
if ( Key.isKeyDown( Key.KEY_R ) )
{
Chat.print( "Reset Movement Animation" );
Actor.resetMovementAnimation();
}
if ( Key.isKeyDown( Key.KEY_I ) )
{
Chat.print( "Setting Idle Animation" );
Actor.setIdleAnimation( "Idle1", -1, Vector(0,0,0) );
}
if ( Key.isKeyDown( Key.KEY_S ) )
{
Chat.print( "Reset Idle Animation" );
Actor.resetIdleAnimation();
}
}
// Add the key listener
Key.addListener( this.oKeyListener );
// Siaplay the instructions
Chat.print( "Press M to change the movement animation to Stealth" );
Chat.print( "Press R to restore movement animation to walk" );
Chat.print( "Press I to change the idle animation to Idle1" );
Chat.print( "Press S to restore the idle animation to Stand" );