0
\$\begingroup\$
void Update () {

        if (goright == true) {
            rig.velocity = new Vector2 (m.movespeed, rig.velocity.y);
            m.anim.SetInteger ("State", 1); 
        }

        if (goleft == true) {
            rig.velocity = new Vector2 (-m.movespeed, rig.velocity.y);
            m.anim.SetInteger ("State", 1);
        }
    }

    public void ondownMoveLeft(){

        goleft = true;
    }

    public void ondownMoveRight(){

        goright = true;
    }

    public void onupMoveLeft(){

        m.anim.SetInteger ("State", 0);
        goleft = false;
    }

    public void onupMoveRight(){

        m.anim.SetInteger ("State", 0);
        goright = false;
    }

i am using this code to move my character using UI buttons with the help of Event Triggers. i set it to change the animation from idle to running or from State 0 to 1 when the player moves and it does works that way but the problem is once it goes into State 1 it never comes back to State 0 but it should since i am setting the State back to 0 when the OnPointerUp trigger gets activated. am i missing something?

OnPointerDown is calling its respective ondown method and OnPointerUp is calling its respective onup method. for eg, when you press the left button it calls OnPointerDown which is set to call the method ondownMoveLeft.

i also checked with Debug.Log and everything is being executed but still the animation State is not changing.

\$\endgroup\$
  • \$\begingroup\$ What OnPointerUp trigger? I don't see it in your code \$\endgroup\$ – UnholySheep Jan 11 '17 at 15:28
  • \$\begingroup\$ its respective "onup" method. \$\endgroup\$ – Mohit Saxena Jan 11 '17 at 15:29
  • \$\begingroup\$ You mean onupMoveRight etc.? How are they being called? They are not part of MonoBehaviour so where are you calling them? \$\endgroup\$ – UnholySheep Jan 11 '17 at 15:30
  • \$\begingroup\$ i am using Event Triggers on each UI button with both OnPointerUp and OnPointerDown events. then using them i call the respective method in this script. \$\endgroup\$ – Mohit Saxena Jan 11 '17 at 15:33
  • \$\begingroup\$ And are they being called properly? Are the m.anim.SetInteger ("State", 1); being executed afterwards? Based on this code sample it will be hard to really help you - you need to use a debugger anyway. \$\endgroup\$ – UnholySheep Jan 11 '17 at 15:37
1
\$\begingroup\$

Since you use Mecanim you need to have two transitions, one from state 0 to state 1 and another from state 1 to state 0.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.