I’m not aware of any way to control something like this in the Animator, I’m pretty sure you’d have to use code or create additional animations. I’ll show one way of doing this for anyone interested.
Here is the code:
public bool halfAnimation = false;
Animator anim;
void Awake()
{
anim = GetComponent<Animator>();
}
void Update()
{
float normalizedTime = anim.GetCurrentAnimatorStateInfo(0).normalizedTime;
float correctNormalizedTime = normalizedTime - Mathf.FloorToInt(normalizedTime);
if (correctNormalizedTime >= 0.5f && halfAnimation == true)
{
anim.Play("Test", 0, 0.5f);
}
}
What this code does is that when the “halfAnimation” bool is set to true, it freezes the animation exactly halfway through its playtime (this is when it reaches at least halfway, it’ll play normally until then). If the bool is false, then the animation plays normally all the way.
How does this work? To explain that, I need to explain how normalized time works.
Normalized time is a value between 0 and 1 that corresponds to an exact point in the animation. 0 is the beginning, 1 is the end, 0.5 is halfway, etc…
We can get this value to see when the animation is halfway done, and we can set an animation to start at any point using this value. The line anim.Play("Test", 0, 0.5f); is constantly playing the “Test” animation starting at normalized time 0.5 (halfway) every frame, so it’s frozen there.
The reason for this calculation…
float correctNormalizedTime = normalizedTime - Mathf.FloorToInt(normalizedTime);
…is because we need to get a normalized time between 0 and 1. The way unity works is that whenever an animation loops, it doesn’t reset its normalized time to 0, it continues adding on past 1. So when the animation is halfway through on its second loop, the normalized time will be 1.5, not 0.5. Because of that, we need to subtract the whole number from the normalized time unity gives us, so that 6.5 would become 0.5, etc…
This is just one way of doing it.