- Home /
If mobile joystick is moved , then animation transforms from idle to walk?
So i need help in my project.
Im trying to recognize joystick movement and then switching unity animations based on it. How can i do this? I have set the bool parameter "isWalking" correctly into animator with transactions.
// Update is called once per frame
void Update()
{
if (Input.GetKey(""){ // IN HERE I AM SUPPOSED TO CONNECT TO JOYSTICK MOVEMENT
anim.SetBool("isWalking", true);
}
else
{
anim.SetBool("isWalking", false);
}
}
}
soo like this? I get the error "cant convert from float to bool" I am a complete novice so it makes things hard haha
void Update()
{
if (Input.GetAxis("Horizontal"){ // IN HERE I A$$anonymous$$ SUPPOSED TO CONNECT TO JOYSTIC$$anonymous$$ $$anonymous$$OVE$$anonymous$$ENT
anim.SetBool("isWalking", true);
}
else
{
anim.SetBool("isWalking", false);
}
}
Answer by ZozeR · Jan 09, 2020 at 09:31 AM
Try using GetAxis instead of getkey. because as far as i know getkey is for getting keyboard input and you have to use keycodes. getaxis is much better since its checking for a lot of input types and highly configurable. you need to use it like this however;
if (Input.GetAxis("Horizontal") > 1)
//right
else if (Input.GetAxis("Horizontal") < 1)
//left
//or
if(Input.GetAxis("Horizontal") != 0)
//move left right
https://docs.unity3d.com/Manual/ConventionalGameInput.html https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
Answer by Tonba · Jan 09, 2020 at 09:58 AM
Hey,
my code is now this:
if (Input.GetAxis("Horizontal") > 1 ){
anim.SetBool("isMoving", true);
}
else if (Input.GetAxis("Horizontal") < 1)
{
anim.SetBool("isMoving", false);
}
}
}
Still no animation changing from idle -> walk.. :/ i have set bool parameter from animator with transactions , bool isMoving true -> goes to walk animation, and if bool is false -> goes back to idle.. Any ideas ?
if horizontal input > 0 it means right input, if its < 0 it means left input.
if (Input.GetAxis("Horizontal") != 0)
{
anim.SetBool("is$$anonymous$$oving",true);
}
else
anim.SetBool("is$$anonymous$$oving",false);
This is the correct way. however i cannot see why it shouldnt work. are you sure unity recognizes your joystick? maybe use Debug.Log(Input.GetAxis("Horizontal")) somewhere to check if it can. if it can; it's probably a problem from your animator. if it can't, check these out; https://docs.unity3d.com/ScriptReference/Input.GetAxis.html https://www.youtube.com/watch?v=p-3S73$$anonymous$$aDP8
Hey man, thank you for your help!
The above code didnt work, i debugged it as u did advice and my game didnt regocnize any movement. i managed to solve my problem and now my animations are playing well when joystick is moved! Heres the code that works:
if (joystick.Horizontal != 0)
{
anim.SetBool("is$$anonymous$$oving", true);
}
else
{
anim.SetBool("is$$anonymous$$oving", false);
}
// Vertical movement detection, if detected setting bool to true -> Animator plays the next animation
if (joystick.Vertical != 0)
{
anim.SetBool("is$$anonymous$$oving", true);
}
else
{
anim.SetBool("is$$anonymous$$oving", false);
}
Your answer
Follow this Question
Related Questions
Animtion looping if joystick is held 0 Answers
Make joystick rotate object 0 Answers
Trigger Animation Via Script 2 Answers
Mobile Joystick Problem 2 Answers
How to script SetTrigger (JS)? 1 Answer