- Home /
The question is answered, right answer was accepted
Unity Getting Input from Horizontal axis while using buttons
I have a good code for my game to move left/right/up/down.I then made on scree controls using some pictures with touch input.The problem im facing is that i cannot move left/right using the buttons.The character is stading in place and i dont know how to solve this please help.
// Check to see if the screen is being touched if (Input.touchCount > 0) { // Get the touch info Touch t = Input.GetTouch(0); // Did the touch action just begin? if (t.phase == TouchPhase.Began) { // Are we touching the left arrow? if (LEFT.HitTest(t.position, Camera.main)) { float move = Input.GetAxis("Horizontal"); rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y); anim.SetFloat("Speed",Mathf.Abs(move)); facingRight = false; } // Are we touching the right arrow? if (RIGHT.HitTest(t.position, Camera.main)) { float move = Input.GetAxis("Horizontal"); rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y); anim.SetFloat("Speed",Mathf.Abs(move)); facingRight = true; } // Are we touching the jump button? if (UP.HitTest(t.position, Camera.main)) { anim.SetBool("Ground",false); anim.SetTrigger("Jumping"); rigidbody2D.AddForce(new Vector2(0,JumpForce)); } if (DOWN.HitTest(t.position, Camera.main)) { anim.SetBool("Crouching",true); } else anim.SetBool("Crouching",false); } // Did the touch end? if (t.phase == TouchPhase.Ended) { // Stop all movement rigidbody2D.velocity = Vector2.zero; }
Answer by Tehnique · Jul 18, 2014 at 11:05 AM
I't not working because you use "Input.GetAxis("Horizontal");". That's only for Keyboards/Joysticks.
You alredy have code to detect if the user is touching your move icons, just set "move" as -1 for left and 1 for right, and remove the GetAxis code.
i have tried this and the character is still standing in place while the animation is starting to run
yes it is decleared public float $$anonymous$$axSpeed = 1f; i also tried values under 1 and same result
Try it with $$anonymous$$axSpeed = 10f, or somthing bigger than 1.
all the checkig gave me the idea and it seems like the problem was i was checking for both keyboard input and touch input with the same variables and that caused the problem.now its all solved ty for trying to help