The first and simplest way:
Copy an animation into 2, set the Speed to -1 and 1 respectively, and then use it normally.
,
an.Play("Unity_Chan_Jump-1");---- call Speed=-1, it is reverse playback
an.Play("Unity_Chan_Jump-1");---call Speed=1, then play in normal order
The second code modifies the Speed method:
1. Same as step setting
2. Code section
public Animator an;
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
if (an.GetCurrentAnimatorStateInfo(0).normalizedTime <= 0)
{
an.SetFloat("speed", 1);
an.Play("Unity_Chan_Jump", 0, 0);
}
else if (an.GetCurrentAnimatorStateInfo(0).normalizedTime > 0)
{
an.SetFloat("speed", 1);
an.Play("Unity_Chan_Jump", 0);
}
}
if (Input.GetKeyDown(KeyCode.D))
{
if (an.GetCurrentAnimatorStateInfo(0).normalizedTime > 1)
{
an.SetFloat("speed", -1);
an.Play("Unity_Chan_Jump", 0, 1);
}
else if (an.GetCurrentAnimatorStateInfo(0).normalizedTime <= 1)
{
an.SetFloat("speed", -1);
an.Play("Unity_Chan_Jump", 0);
}
}
}