Search Unity

Question Calling animation states from code

Discussion in 'Animation' started by lz7cjc, Jun 15, 2021.

  1. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    541
    Hi
    I promise I have been trying to find the answer to this but I think it is too simple to be covered. So hoping someone here can help

    I have two animation states:
    sittingidle
    talking

    When the user chooses the avatar they should switch from sittingidle to talking and when they lose focus it should return to the sittingidle pose

    I have set up a controller with both states; sittingidle is default and this plays correctly on my avatar

    I have tried
    public AnimationClip[] animationClips;
    private RuntimeAnimatorController controller;
    private Animator animator;

    but can't make any of them work

    How do i switch in code?

    thanks
    V2019.4.0f1
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Assign the Animator Controller to the Controller field in the Animator component's Inspector.

    Then call animator.Play("State Name");
     
    lz7cjc likes this.