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;
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.
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).
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.
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)) {
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?
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.
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
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.