- Home /
Unity touch not working correctly
Hi. So my problem is that when i drag one finger on the left side of the screen to move the gameobject it moves correctly. But when a touch the right side of the screen to jump, it stops the movement of the object in the left or right direction. Can anybody help me solve this? Here's the code:
Btw "move" is covering up the whole left side of the screen(transparent) and "jump" is covering up the whole right side of the screen. void Update () {
for(int i=0; i< Input.touchCount;i++){
if((Input.GetTouch(i).phase == TouchPhase.Moved || Input.GetTouch(i).phase == TouchPhase.Began || Input.GetTouch(i).phase == TouchPhase.Stationary) && Input.touchCount >0)
{
myRay = Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);
{
if (Physics.Raycast(myRay, out rayHit ))
{
if(rayHit.collider.tag == "move")
{
for (int ii = 0; ii < Input.touchCount; ++ii) {
Touch touch2 = Input.GetTouch(ii);
if (touch2.phase == TouchPhase.Began) {
touch1 = touch2.position.x;
}
if (touch2.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Stationary) {
if(touch2.position.x > touch1){
GameObject.Find ("player").transform.Translate (Vector3.right * moveSpeed * (Time.deltaTime) * 5);
}
if(touch2.position.x < touch1){
GameObject.Find ("player").transform.Translate (Vector3.left * moveSpeed * (Time.deltaTime) * 5);
}
}
}
}
if(rayHit.collider.tag == "jump")
{
if (time > 70) {
GameObject.Find ("player").rigidbody2D.AddForce (transform.up * jumpPower);
time = 0;
}
}
}
}
}
}
} }
Thanks for your help
Comment