I’ve animations which can be performed by way of script. Sometimes, glitches happen at runtime that I am unable to clarify. Right here is an instance:
These glitches don’t happen persistently however seem in virtually all animations. They appear to occur solely when one other animation is enjoying concurrently. As proven within the define, the animation itself needs to be appropriate. Right here is the code used to play the animations:
non-public void PlayState(string tag, bool stopAfter = true)
{
animator.enabled = true;
if (!HasState(tag))
{
Debug.LogWarning($"No state with tag {tag} discovered.");
return;
}
var stateInfo = States(tag).Random();
if (stateInfo == null)
{
Debug.LogError($"No state with tag {tag} present in checklist.");
return;
}
animator.Play(stateInfo.Title,0,0);
currentState = stateInfo;
if (stopAfter)
StartCoroutine(WaitUntilFinished(stateInfo));
}
non-public IEnumerator WaitUntilFinished(StateInfo data)
{
var shortNameHash = Animator.StringToHash(data.Title);
// Wait till animation is definitely performed.
yield return new WaitUntil(() => shortNameHash == animator.GetCurrentAnimatorStateInfo(0).shortNameHash);
var cur = animator.GetCurrentAnimatorStateInfo(0);
if (cur.loop) yield break;
yield return new WaitForSeconds(cur.size);
if(currentState != data) yield break;
Cease();
}
public void Cease()
{
spriteRenderer.sprite = defaultSprite;
animator.enabled = false;
}
I’ve already tried eradicating animations that I suspected have been defective, however this conduct happens with many various ones. Subsequently, I think the difficulty lies in my script. How can I stop these glitches?