Q: Simple animation condition trigger on menu button?

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!

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?)

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

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.

Okay, I understand now. Did @Suddoha 's answer get it for you?

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 :wink:

rob

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.

Hi,

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OutIn : MonoBehaviour
{
    public bool outin;
    Animator AnimTrigger;

    void Start()
    {
        AnimTrigger = GetComponent<Animator>();

    }

    public void OnClick()
    {
        outin = !outin;
        if (outin)
        {
            outin = true;
            AnimTrigger.SetTrigger("animtrigger");
            Debug.Log("OUT");
        }
        else
        {
            outin = false;
            AnimTrigger.SetTrigger("animtrigger");
            Debug.Log("IN");
        }
    }
}

If there’s a cleaner way of doing it, I’m all ears :wink:

cheers,

rob

I’m glad you got it working. Are you using ‘outin’ somewhere else?

Unless I am mistaken, does your method OnClick() not just boil down to this? :

public void OnClick()
{
    AnimTrigger.SetTrigger("animtrigger");
}

:slight_smile: Indeed… that’s why I asked if that variable is used, just in case.

I was just editing my comment to include your point - you beat me to it! :smile:

It’s all good. :slight_smile:

Ah…

Even shorter code! :slight_smile:

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

rob