Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. We are looking for your feedback about Templates! Tell us about your experiences by taking our survey.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Cycling animations with two UI Buttons

Discussion in 'Vuforia' started by zara2355, Jun 29, 2020.

  1. zara2355

    zara2355

    Joined:
    Nov 17, 2019
    Posts:
    8
    Hello,

    Im certain this is a pretty easy problem, but Im still very new and dont understand scripting very well yet, so apologies in advance.

    What I am trying to do is simply cycle through a set of 7-8 animations using two UI buttons ("Next" and "Previous")

    Im not entirely sure what to do. I have figured out how to set a transition and trigger it, but it seems the "On Click" function only allows one transition. Additionally, in my script, I sort of feel like I should be calling on items in an array, but really have no idea how to write this.

    Heres my script so far, but im not sure its even in the right direction. Thanks in advance

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Vuforia;
    5.  
    6. public class NextButton : MonoBehaviour
    7. {
    8.  
    9.     public Animator anim1;
    10.    // public GameObject AdvanceAni;
    11.  
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         anim1 = GetComponent<Animator>();
    17.         //anim1.Speed = 0f;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23. /*        if (Input.GetButtonDown ("anim1"))
    24.         {
    25.  
    26.             anim1.SetTrigger("Next1");
    27.             Debug.Log("HELLLLLLLOOOOO");
    28.  
    29.         }*/
    30.  
    31.     }
    32.  
    33.     public void AnimAdvance1()
    34.     {
    35.  
    36.         anim1.Play("anim1");
    37.         // anim1.Speed = 1f;
    38.         //anim1.Play("anim2");
    39.     }
    40.  
    41.     public void AnimAdvance2()
    42.     {
    43.  
    44.  
    45.         // anim1.Speed = 1f;
    46.         anim1.Play("anim2");
    47.     }
    48.  
    49. }
    50.  
     
  2. zara2355

    zara2355

    Joined:
    Nov 17, 2019
    Posts:
    8
    Hello, I made some progress, however Im having issues with increment/decrement.

    the increment seems to work ok, and loops back the first animation, which is good, but the "previous" button doesnt go back one animation. Rather, it also increments instead of decrements. Im stumped.


    I fell like im probably close to the solution, but i really have no idea if im doing this correctly. New script below. Any help would be greatly appreciated!!!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Vuforia;
    5.  
    6. public class NextButton : MonoBehaviour
    7. {
    8.  
    9.     public AnimationClip[] ac;
    10.     public Animator ani;
    11.     int index = 0;
    12.  
    13.     void Start()
    14.     {
    15.         ani = GetComponent<Animator>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.  
    21.     }
    22.  
    23.     public void AnimNext()
    24.     {
    25.         index++;
    26.         ani.Play(ac[index].name);
    27.         if (++index == ac.Length)
    28.         {
    29.             index = 0;
    30.  
    31.         }
    32.         Debug.Log("HELLLLLLLOOOOO");
    33.     }
    34.  
    35.     public void AnimPrevious()
    36.     {
    37.         index--;
    38.         ani.Play(ac[index].name);
    39.         if (--index == 0)
    40.         {
    41.             index = 2;
    42.         }
    43.  
    44.         Debug.Log("BYYYYYYEEEEE");
    45.     }
    46.  
    47. }
    48.  
     
  3. zara2355

    zara2355

    Joined:
    Nov 17, 2019
    Posts:
    8
    Welp, in case anyone else out there is in my same situation and is banging their head against the proverbial wall, I figured it out (along with learning about out of array bounds, so yay me). THis script allowed me to cycle through animations (Next, Repeat Same animation, go back to the previous animation).
    I hope it helps *someone* out there

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Vuforia;
    5.  
    6. public class NextButton : MonoBehaviour
    7. {
    8.  
    9.     public AnimationClip[] ac;
    10.     public Animator ani;
    11.     int index = 0;
    12.  
    13.     void Start()
    14.     {
    15.         ani = GetComponent<Animator>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.  
    21.     }
    22.  
    23.     public void AnimNext()
    24.     {
    25.  
    26.         if (index == 4)
    27.         {
    28.             ani.Play("anim0");
    29.             index = 0;
    30.         }
    31.         else
    32.         {
    33.             index = (index + 1);
    34.             ani.Play(ac[index].name);
    35.         }
    36.  
    37.         Debug.Log(ac[index].name);
    38.     }
    39.  
    40.  
    41.  
    42.     public void AnimRepeat()
    43.     {
    44.         if (index == 0)
    45.         {
    46.             ani.Play("anim0");
    47.             index = 0;
    48.         }
    49.         else
    50.         {
    51.             ani.Play(ac[index].name, -1, 0f); //THIS REPEATS THE CURRENT ANIMATION (-1, 0f)
    52.         }
    53.  
    54.         Debug.Log(ac[index].name);
    55.     }
    56.  
    57.     public void AnimPrevious()
    58.     {
    59.         if (index == 0)
    60.         {
    61.             ani.Play("anim0");
    62.             index = 0;
    63.         }
    64.         else
    65.         {
    66.             index = (index - 1);
    67.             ani.Play(ac[index].name);
    68.         }
    69.  
    70.         Debug.Log(ac[index].name);
    71.     }
    72. }
    73.  
     
unityunity