Table of Contents

Animating a character

This section guides you through how you animate characters in TMPEffects.

As shown in Creating animations, the Animate method takes two parameters:

For general information about these types, see the respective documentation.

Applying transformations to a character

Moving the character

To move the character, simply use the SetPosition(Vector3 position) or AddPositionDelta(Vector3 delta) method on CharData.
Most of the time, you will want to do this using the original position of the character and an offset.

public void Animate(CharData cData, IAnimationContext context)
{
    // Move the character up 125 units over time, then down again; indefinitely
    float val = Mathf.PingPong(context.animatorContext.PassedTime * 50, 125);
    cData.SetPosition(cData.InitialPosition + Vector3.up * val);
}

Rotating the character

To rotate the character, use CharData's SetRotation(Quaternion rotation) method. If you want to rotate around a specific pivot, you may set it using either the SetPivot(Vector3 pivot) method or the AddPivotDelta(Vector3 delta) method.
If you don't set a pivot for the rotation, it will rotate around the center of the character.

public void Animate(CharData cData, IAnimationContext context)
{
    // Rotate the character indefinitely in the z axis over time
    float angle = context.animatorContext.PassedTime * 50 % 360;
    cData.SetRotation(Quaternion.Euler(0, 0, angle));

    // And by adding this line, it will use the pivot you set;
    // in this case the character will rotate around the point 150 units from its
    // center on the x axis
    cData.AddPivotDelta(Vector3.right * 150);
}

Scaling the character

To scale the character, use the SetScale(Vector3 scale) method.

public void Animate(CharData cData, IAnimationContext context)
{
    // Ping-pong the scale between (0, 0, 0) and (1, 1, 1) over time
    float val = Mathf.PingPong(context.animatorContext.PassedTime, 1);
    Vector3 scale = Vector3.one * val;
    cData.SetScale(scale);
}

Modifying a character's vertices

Setting the vertices' positions

You can set the positions of the character's vertices using the SetPosition(int i, Vector3 value) method on the VertexData type. The integer specifies the vertex to modify (again, see CharData) while the vector specifies the new position.

public void Animate(CharData cData, IAnimationContext context)
{
    // Pingpong the magnitude of the offset between 0 and 125 over time,
    // then add that offset to the two top vertices 
    float val = Mathf.PingPong(context.animatorContext.PassedTime * 50, 125);
    for (int i = 1; i < 3; i++)
    {
        cData.mesh.SetPosition(i, cData.initialMesh.GetPosition(i) + Vector3.up * val);
    }
}

Setting the vertices' colors

Using the SetColor(int i, Color32 value, bool ignoreAlpha) method, you can set the color value of each vertex.
If you pass true for ignoreAlpha, only the RGB color channels are overwritten; the alpha channel will remain unchanged.
If you want to do the opposite of this and only set the alpha channel, then you can use the SetAlpha(int i, float alpha) method.

public void Animate(CharData cData, IAnimationContext context)
{
    // Set each vertex color to red and set the alpha dependent on passed time.
    Color32 color = Color.red;
    float alpha = Mathf.PingPong(context.animatorContext.PassedTime * 125, 255);
    for (int i = 0; i < 4; i++)
    {
        cData.mesh.SetColor(i, color, true);
        cData.mesh.SetAlpha(i, alpha);
    }
}

Setting the vertices' UVs

Using the SetUV0(int i, Vector2 uv) and SetUV2(int i, Vector2 uv) methods, you can set the UV0 and UV2 values of each vertex respectively.

These properties are more niche compared to the other ones, and you will likely use them much less; char is the only built-in animation to utilize this property.

public void Animate(CharData cData, IAnimationContext context)
{
    // Pan the UV0 of the character over time
    Vector2 delta = Vector2.right * context.animatorContext.PassedTime;
    for (int i = 0; i &lt; 4; i++)
    {
        cData.mesh.SetUV0(i, cData.initialMesh.GetUV0(i) + delta);
    }
}

Making your animation fancier

The above character transformations and vertex modifiers are all you really need to animate your character!
Combined with easing functions (see: AnimationCurveUtility, Waves), even these really simple animations above can look quite nice already.

For example, the animation code for the built-in jump animation is hardly more complicated than that for the first animation on this page (at least once you've looked at Waves 😉).

public override void Animate(CharData cData, IAnimationContext context)
{
    Data data = (Data)context.customData;

    float eval = data.wave.Evaluate(context.animatorContext.PassedTime, GetWaveOffset(cData, context, data.waveOffsetType)).Item1;
    cData.SetPosition(cData.InitialPosition + Vector3.up * eval);
}