Search Unity

GUI button will move but won't animate my game object.

Discussion in 'Scripting' started by Astral14, Sep 23, 2014.

  1. Astral14

    Astral14

    Joined:
    Aug 5, 2014
    Posts:
    21
    Hi,

    In my 2d game have a GUI texture button and when clicked the character must move right and animate running.
    My script does move the character but it will not animate, so no running animation the character just moves to the right. If I attach the same script to my character instead of the GUI button and replace OnMouseDown with fixedupdate, than my character moves right and animates running. The script is below, Can anyone help me with this? Thanks :)

    public float speed = 10f;
    private GameObject bunny;
    Animator anim;

    void Start()
    {
    bunny = GameObject.Find("Bunny");
    anim = bunny.GetComponent<Animator> ();
    }

    void OnMouseDown()
    {
    float mx = Input.GetAxis ("Horizontal");
    anim.SetFloat ("Speed", Mathf.Abs (mx));
    bunny.rigidbody2D.velocity = new Vector2 (speed, bunny.rigidbody2D.velocity.y);
    }
    }
     
  2. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I could be reading this wrong, but you could try something along the lines of anim.SetFloat("Speed" +=1)... Change 1 to whatever you have your float in Animator set to to activate it, OR make a bool which is just easier if you ask me...

    anim.SetBool("whatever" true)

    else

    anim.SetBool("whatever" false)

    that kinda thing..

    If all else fails set up debug.logs after certain things to see if it's even being called correctly.

    Hope that helps!
     
    Astral14 likes this.
  3. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    You try to set an int in here "anim.SetFloat ("Speed", Mathf.Abs (mx));"
    I am not sure if that's the issue, but try to set it like that;
    anim.SetFloat ("Speed", Mathf.Abs (mx) * 1f);

    Also, I find it weird that you get the axis while "OnMouseDown". You would need to press the movement key, and while doing that, press the button, isn't that right?

    In other words, check the value of "mx" while testing it.

    EDIT: My bad, for some reason I thought of abs like "roof" or whatever it is named. I think the only issue is the mx thing, it does not make much sense, in my opinion, to use "GetAxis" only at the split second that a gui button is pressed (or while it's pressed, still uncomfortable).
     
    Last edited: Sep 23, 2014
  4. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    You make some valid points as well... Likewise, he could easily just tell it to make the transform move in the facing direction or using AddForce as well if it's a rigidbody and just have it play the Animation when he presses the button.
     
  5. Astral14

    Astral14

    Joined:
    Aug 5, 2014
    Posts:
    21
    Hi,

    Awesome! Your suggestion make a bool instead of float worked :) The object now moves and animates running, though had to add OnMouseup so the object stops moving when the animation stops. However now when I add another GUI texture button which when clicked this object will move in the opposite direction doesn't work. To move in the opposite direction, speed variable is set to -10 and I have created a function Flip which will change the scale of my character to -1. I will add this Flip function in FixedUpdate. below is the code,

    void FixedUpdate()
    {
    if (Input.GetMouseButtonDown (0)) {

    Flip ();
    anim.SetBool ("Click", true);
    hero.rigidbody2D.velocity = new Vector2 (speed, hero.rigidbody2D.velocity.y);
    }
    }

    void Flip()
    {
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    hero.transform.localScale = theScale;
    }

    However now when I click the button to flip my character and move in opposite direction my character simply disappears, in the inspector the scale automatically changes to 0 (instead of -1) and the positions have changed too! Do you know what's the problem with the code, why it wont work if I have to move in opposite direction?
     
  6. Astral14

    Astral14

    Joined:
    Aug 5, 2014
    Posts:
    21
    Yes you are right axis and float parameter does not work when a GUI Texture button is clicked. I tried to set bool parameter - it was suggested by N1warhead- and it worked. Now my character moves and animates.
     
    Last edited: Sep 24, 2014
  7. Astral14

    Astral14

    Joined:
    Aug 5, 2014
    Posts:
    21
    Hi, Ignore the message I wrote earlier about the problem I was facing if I move in opposite direction. The problem was with my code, this is wrong, theScale.x *= -1;
    .....instead it should be... theScale.x = -1; now it works :)

    N1warhead,Thank you so much, your suggestion to use a bool made my day :) :p
     
  8. N1warhead

    N1warhead

    Joined:
    Mar 12, 2014
    Posts:
    3,884
    I'm glad it is working for you now!
    I love the satisfaction of helping others, makes me feel good lol.
    But I'm glad its working for you. If you have any other problems send me a PM and I'll try to get back to you as soon as I can.