Scripting an animation clip
Overview
This guide provides information on how to find an animation clip in Asset Library, configure it in your Space, and play it using a LUA script.
Steps to Follow
- 
Finding an appropriate animation clip
- Navigate to Asset Library to browse available animation clips.
 - Look for and add the animation clip titled "Wave Hip Hop Dance_Wave Hip Hop Dance."
 
 - 
Selecting a suitable model
- Since the animation clips in Asset Library are designed for humanoid models, locate an appropriate model.
 - Find the humanoid model named "BackRoomsCharacter01" and place it in your Space.
 
 - 
Binding the animation clip to the model
- 
To bind the selected animation clip to your model, you will need a community component called "Animation-play ability."
 - 
This component can also be found in the Asset Library.
 - 
Attach this component to your chosen model and select the animation clip you wish to bind.
 
 - 
 - 
Converting the model into an animator controller
- Only an animator controller can play animation clips. Convert your model into an AnimatorController by making it a non-player character (NPC).
 - This can be done by attaching a Monster component, which designates the model as an NPC.
 
 - 
Playing the Animation Create a script and attach it to the model that has been added to your Space.
Script Setup:
- Create an entity for your model in the scene.
 - Check if the entity is an animator controller, as only animator controllers can play animation clips.
 
local _selfEntity = script:SelfEntity()
if not YaAnimatorAPI.IsAnimatorController(_selfEntity) then
return
end- To play the animation clip, retrieve the AnimatorControllerEntity using the entity and call the play function with the name of your animation clip:
 
local _animatorControllerEntity = YaAnimatorAPI.Instance(_selfEntity)
local _handle = _animatorControllerEntity:Play("Wave Hip Hop Dance_Wave Hip Hop Dance")- Set the animation to loop:
 
_handle:SetLooping(true) - 
Triggering the animation clip To trigger the animation when certain events occur, add an event listener in your script:
local _selfEntity = script:SelfEntity()
function OnAvatarSpawned(playerId, spawnPointEntity, avatarEntity)
if not YaAnimatorAPI.IsAnimatorController(_selfEntity) then
return
end
local _animatorControllerEntity = YaAnimatorAPI.Instance(_selfEntity)
local _handle = _animatorControllerEntity:Play("Wave Hip Hop Dance_Wave Hip Hop Dance")
_handle:SetLooping(true)
end
function OnPlayerJoined(playerId)
local player = YaGame:GetPlayer(playerId)
EventHelper.AddListener(player, "SpawnedEvent", OnAvatarSpawned)
end
EventHelper.AddListener(YaGame, "PlayerJoinedEvent", OnPlayerJoined) - 
Save your script and enter Play Mode to test your setup.