Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Q: Simple animation condition trigger on menu button?

Discussion in 'Scripting' started by Deleted User, Apr 27, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    I'm looking at this atm, but cannot seem to find a solution so far.

    What I want is to call a animation trigger between animations A and B.
    So click once, the transition trigger is set and animation B is played. Click again and trigger sets animation back to A. This works manually when running the game window.

    I found something working for a mousebutton click to run anim.SetTrigger("Triggername"), but cannot seem to find any examples how to set this up for a UI menu button.
    Found lots of UI trigger examples, and some mentioning of Events, but nothing directly on a menu button.

    cheers!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is your question like this, just for clarification:
    2 trigger variables "trigger1" and "trigger2"

    click the button for the first time, do trigger1.
    Second time, do trigger2.
    (third time? back to trigger1?)
     
  3. Deleted User

    Deleted User

    Guest

    Hi,

    No, just one trigger on both transitions from A>B>A.
    So default state > trigger1 > animB > trigger1 > default state.

    Basically a on/off switch.
    Like I said, I cannot seem to find something that would be such a basic thing?

    cheers,

    rob
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    If you have already found a solution that works with mouse buttons, you should be able to apply the same to menu buttons. It's just a different way of associating the logic with the input control.
    With mouse buttons, you've probably polled the button state via Unity's standard input in Update. With Unity's UI system (not the legacy UI though), you'd add a listener to the onClick event.

    The rest will be pretty much the same.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, I understand now. Did @Suddoha 's answer get it for you?
     
  6. Deleted User

    Deleted User

    Guest

    Hi,

    A bit of a late reply, but Suddoha's reply got me in the right direction in the end.

    I thought I found a way to do some it without code, by dragging the object/hierarchy onto the button's onClick event, and selecting the Gameobject:SetActive(bool) function.
    Unfortunately this doesn't really do a boolean on/off switch., but it is a quick way to do simple stuff straight from the Inspector.

    Still wrapping my head around coding in Unity ;)

    rob
     
    Last edited by a moderator: Apr 30, 2018
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well, that's quite different than setting an animitor's trigger.

    Post the code you used with the mouse button, and we will tell you how to set this up with the UI button.
     
  8. Deleted User

    Deleted User

    Guest

    Hi,

    Worked it out this way.
    Simple two way transition with a trigger.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OutIn : MonoBehaviour
    6. {
    7.     public bool outin;
    8.     Animator AnimTrigger;
    9.  
    10.     void Start()
    11.     {
    12.         AnimTrigger = GetComponent<Animator>();
    13.  
    14.     }
    15.  
    16.     public void OnClick()
    17.     {
    18.         outin = !outin;
    19.         if (outin)
    20.         {
    21.             outin = true;
    22.             AnimTrigger.SetTrigger("animtrigger");
    23.             Debug.Log("OUT");
    24.         }
    25.         else
    26.         {
    27.             outin = false;
    28.             AnimTrigger.SetTrigger("animtrigger");
    29.             Debug.Log("IN");
    30.         }
    31.     }
    32. }
    If there's a cleaner way of doing it, I'm all ears ;)

    cheers,

    rob
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm glad you got it working. Are you using 'outin' somewhere else?
     
  10. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Unless I am mistaken, does your method OnClick() not just boil down to this? :
    Code (CSharp):
    1. public void OnClick()
    2. {
    3.     AnimTrigger.SetTrigger("animtrigger");
    4. }
    5.  
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    :) Indeed.. that's why I asked if that variable is used, just in case.
     
  12. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I was just editing my comment to include your point - you beat me to it! :D
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It's all good. :)
     
  14. Deleted User

    Deleted User

    Guest

    Ah...

    Even shorter code! :)

    Thanks all, much appreciated!
    Getting the hang of C# is a bit of a rough ride atm, coming from 3D, but getting there slowly ;)

    rob