- Home /
This question was
closed Jul 22, 2014 at 01:06 PM by
RedDevil for the following reason:
I figured it out by myself after a few hours
Question by
RedDevil · Jul 22, 2014 at 08:22 AM ·
inputtouchcontrollertouchscreentouchphase
Unity 2D Touch Controll via on screen button for Mobile
Hi.I have implemented touch controll over my game from diferent scripts I found on the web to be able to jump and move left/right via buttons on screens(im using GUI Textures i created).The problem is the script I ended up with is not very responsive and it just seems wierd. It all worked perfect on the computer when I was using Input.GetAxis horizonal but when I try via code it just does not have the same feel to it.Curently my problem is with Left/Right movement. Here is my code:
void FixedUpdate() { grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround); if(move < 0 && facingRight) { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } if(move > 0 && !facingRight) { facingRight = !facingRight; Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } // 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 right arrow? if (RIGHT.HitTest(t.position, Camera.main)) { move += 0.99f; rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y); anim.SetFloat("Speed",Mathf.Abs(move)); if(move > MaxSpeed) { move = MaxSpeed; } } // Are we touching the left arrow? if (LEFT.HitTest(t.position, Camera.main)) { move -= 0.99f; rigidbody2D.velocity = new Vector2(move * MaxSpeed,rigidbody2D.velocity.y); anim.SetFloat("Speed",Mathf.Abs(move)); if(move > MaxSpeed) { move = MaxSpeed; } } // Are we touching the jump button? if (UP.HitTest(t.position, Camera.main) && grounded) { anim.SetBool("Ground",false); anim.SetBool("Jumping",true); rigidbody2D.AddForce(new Vector2(0,JumpForce)); } if (DOWN.HitTest(t.position, Camera.main)) { anim.SetBool("Crouching",true); } } // Did the touch end? if (t.phase == TouchPhase.Ended) { anim.SetBool("Crouching",false); anim.SetBool("Jumping",false); anim.SetFloat("Speed",0); move = 0; // Stop all movement rigidbody2D.velocity = Vector2.zero; } } if(grounded && Input.GetKeyDown(KeyCode.Z)) { move -= 10; rigidbody2D.velocity = Vector2.right * -5; anim.SetFloat("Speed",Mathf.Abs(-5)); } if(grounded && Input.GetKeyUp(KeyCode.Z)) { move = 0; anim.SetFloat("Speed",0); } if(grounded && Input.GetKeyDown(KeyCode.X)) { move = 10; rigidbody2D.velocity = Vector2.right * 5; anim.SetFloat("Speed",Mathf.Abs(5)); } if(grounded && Input.GetKeyUp(KeyCode.X)) { move = 0; anim.SetFloat("Speed",0); } if(grounded && Input.GetKeyDown(KeyCode.Space)) { anim.SetBool("Ground",false); anim.SetBool("Jumping",true); rigidbody2D.AddForce(new Vector2(0,JumpForce)); } if(grounded && Input.GetKeyUp(KeyCode.Space)) { anim.SetBool("Jumping",false); } if(grounded && Input.GetKeyDown(KeyCode.C)) { anim.SetBool("Crouching",true); } if(grounded && Input.GetKeyUp(KeyCode.C)) { anim.SetBool("Crouching",false); } }
I am using Z and X for left and right so I dont have to keep building versions for iPad to test.Hope you can help me.Thank you
Comment