Animation States
Summary
Control animation states from a Reactor server using entity properties as animation parameters linked to animation transitions in an animation controller.
Setup
- Import 4 animations. 'Idle', 'Walk', 'Run', and 'Cheering' and configure the following properties on all animations
Loop Time = Checked
Root Tranform Rotation
Based Into Pose = Checked
Based Upon = Body Orientation
Root Tranform Position Y
Based Into Pose = Checked
Based Upon (at Start) = Original
Root Tranform Position XZ
Based Into Pose = Checked
Based Upon (at Start) = Original
- Create an animation controller and add the imported animations. Add a float parameter named
Speed
and a bool parameter namedCheering
to the animation controller. Add transitions between the animation states with conditions based on the two parameters:
Idle (Linked from Entry)
Transition To Walk, exit time disabled, Speed > 0.01 condition.
Transition To Cheering, exit time disabled, Cheering = True condition.
Walking
Transition To Idle, exit time disabled, Speed < 0.01 condition.
Transition To Run, exit time disabled, Speed > 1 condition.
Transition To Cheering, exit time disabled, Cheering = True condition.
Running
Transition To Walk, exit time disabled, Speed < 1 condition.
Transition To Cheering, exit time disabled, Cheering = True condition.
Cheering
Transition To Idle, exit time disabled, Cheering = False condition.
- Create an NPC game object from the idle animation asset. Set the NPC animator controller reference to the animation controller created above.
- Add a Reactor ksEntityComponent to the NPC and uncheck the 'Is Permanent' property.
- Add a ksAnimationSync to the NPC. Expand the 'Animation Properties' section and make note of the properties. The number before the property is the id we will use to set the property value on the server. The 'Speed' property should have id 1000 and 'Cheering' should have id 1001.
- You can change their ids by changing the starting value for 'Animation Property Ids'. If you change their ids, be sure to change the ids in the script you create for the next step.
- Add a Reator server entity script named
seAnimator
to the NPC. This script sets the entity property 1000 ('Speed') value and the entity property 1001 ('Cheering') property during an update loop. The script changes the speed during the update loop and periodically sets and unsets the cheering property. This is just an example script, actual scripts used in games will drive animation states in different ways.
seAnimator.cs
using KS.Reactor.Server;
public class seAnimator : ksServerEntityScript
{
private float m_cheerDuration = 0f;
private float m_cheerTime= 10f;
private float m_speed = -1f;
private bool m_increaseSpeed = true;
public override void Initialize()
{
Room.OnUpdate[0] += Update;
}
public override void Detached()
{
Room.OnUpdate[0] -= Update;
}
private void Update()
{
// Ping pong speed up and down to switch between idle, walking and running
if (m_speed < -1.0f)
m_increaseSpeed = true;
else if (m_speed > 2.0f)
m_increaseSpeed = false;
m_speed += 0.5f * (m_increaseSpeed ? Time.Delta : -Time.Delta);
Entity.Properties[1000] = m_speed;
//Start Cheering
m_cheerTime -= Time.Delta;
if (m_cheerTime <= 0)
{
m_cheerTime = 10.0f;
m_cheerDuration = 2.0f;
Entity.Properties[1001] = true;
}
// Stop Cheering
if (m_cheerDuration > 0)
{
m_cheerDuration -= Time.Delta;
if (m_cheerDuration <= 0)
{
Entity.Properties[1001] = false;
}
}
}
}