Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Playing animations through script.

Discussion in 'Animation' started by cstehreem, Feb 27, 2015.

  1. cstehreem

    cstehreem

    Joined:
    Oct 31, 2013
    Posts:
    9
    I am developing an animation for a Rubik's Cube. I have created a cube, an empty game object 'Rotator' and 6 animations to perform clockwise and counter clockwise rotations. Now when I need to rotate a particular face I make relevant blocks Rotator's child elements and play animation on rotator. Afterwards I remove all the blocks from Rotator.

    This is my relevant code:
    public void TopCross()
    {
    Animator anim = gameObject.GetComponent<Animator>();
    SelectFace("Right");
    anim.SetTrigger("XC"); //XC is trigger for clockwise rotation around X axis
    UnSelect();
    SelectFace("Left");
    anim.SetTrigger("ZCC"); //trigger for counter clockwise rotation around Z axis
    UnSelect();
    }

    My problem: Evident enough I need to wait for the first animation to finish before I trigger the next one. I have tried working with coroutine but I couldn't quite understand how to use it in my case because I have to rotate a number of faces a couple times.
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    there is a few thing you could do.

    1. Use animation event to mark the end of your clip and when you get the callback you start the next animation.
    2. Make sure that you clip properties loop pose and loop time is uncheck, then you can use
    http://docs.unity3d.com/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html
    to detect when you clip is finish like this
    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.     if(animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f)
    5.     {
    6.         // do your stuff here
    7.     }
    8. }
    9.  
    3. If you have 5.0 beta you can use StateMachineBehaviour.OnStateExit callback to detect such case.
     
  3. cstehreem

    cstehreem

    Joined:
    Oct 31, 2013
    Posts:
    9
    I followed your advice and my code looks like this now:

    Code (CSharp):
    1.  
    2. void Update () {
    3.         if(anim.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1.0f && !isPlaying)
    4.         {
    5.             if(pointer < steps.Count)
    6.             {
    7.                 temp = pointer;
    8.                 pointer++;
    9.                 isPlaying = true;
    10.                 SelectFace(faces[temp]);        //takes a face from the list of faces
    11.                 anim.SetTrigger(steps[temp]);      //takes step name from list of steps
    12.             }
    13.         }
    14.     }
    15.    
    16.     public void UnSelect()
    17.     {
    18.         GameObject[] blocks;
    19.         blocks = GameObject.FindGameObjectsWithTag("StaticCube_Block");
    20.         foreach(GameObject block in blocks)
    21.         {
    22.             block.transform.parent = gameObject.transform;
    23.         }
    24.         isPlaying = false;
    25.     }
    26.  
    I am calling UnSelect() function at the end of every clip. However, sometimes it does not complete execution and hence the next clip plays on wrong blocks. How can I make sure that doesn't happen?
     
  4. cstehreem

    cstehreem

    Joined:
    Oct 31, 2013
    Posts:
    9
    It seems like the animation gets cut off in the middle and UnSelect() doesn't get called at all