- Home /
The question is answered, right answer was accepted
How to play only one frame of animation using and changing RuntimeAnimatorController
Hello! I want to play only one frame of an animation clip inside a RuntimeAnimatorController that is an AnimatorOverrideController. I'm using 2d.
I get no errors on console but the sprite does not change. I used a prefab and assigned the RuntimeAnimatorControllers.
Edit: I need to use the others animator controllers to change the animation too.
My current code is:
public class Sample : MonoBehaviour
{
public RuntimeAnimatorController animator2;
public RuntimeAnimatorController animator3;
public float timeBetweenChanges = 0.3f;
private Animator _animator;
public enum MyAnimatorTypes
{
animator2 = 0,
animator3 = 1
}
void Start()
{
_animator = (Animator)GetComponent(typeof(Animator));
}
IEnumerator _crChangeType()
{
yield return new WaitForSeconds(timeBetweenChanges);
_ChangeType();
StartCoroutine(_crChangeType());
}
private void _ChangeType()
{
MyAnimatorTypes newType = MyAnimatorTypes.animator2;
switch (newType)
{
case MyAnimatorTypes.animator2:
_animator.runtimeAnimatorController = animator2;
break;
case MyAnimatorTypes.animator3:
_animator.runtimeAnimatorController = animator2;
break;
}
AnimatorStateInfo asi = _animator.GetCurrentAnimatorStateInfo(0);
_animator.Play(asi.shortNameHash, 0, 0f);
}
}
I'dv try setting a bool to true after playing the animation, then in the same Update function (Update, FixedUpdate or LateUpdate) if the bool is set to true, stop the animation or play anohter one (and set the bool to false again). This should theoretically play the animation for one frame, which is the time from one of the Update functions to the next.
Thank you! But the current code does not play the animation yet... I don't know why...
Answer by dwyffq · Nov 22, 2017 at 04:50 PM
Sorry. This part of code is working. I had more code and forgot to set some things in inspector