How to make an invisible area for the android on the screen (the right half of the screen is the left half of the screen), by tapping on which you can call the necessary script (i.e. to control the GO). This script is not working here.

using UnityEngine; using System.Collections; public class controler : MonoBehaviour { public GameObject player; private Touch touch; void Update() { int fingerCount = 0; foreach (Touch touch in Input.touches) { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) fingerCount++; } if (fingerCount > 0) { if (touch.position.x < (Screen.width / 2)) player.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y - 90f, 0); } else player.transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y + 90f, 0); } } 

Why, what is not right? Thank you all.

    2 answers 2

    The script below rotates the object to the right - to the left, depending on the side of the screen that was touched.

     using UnityEngine; using System.Collections; public class turner : MonoBehaviour { public GameObject player; private bool rot = false; void Update() { { if (Input.touchCount == 0) rot = false; if (rot) return; if (Input.touchCount > 0) if (Input.touches[0].phase != TouchPhase.Ended && Input.touches[0].phase != TouchPhase.Canceled) { rot = true; if (Input.touches[0].position.x > (Screen.width / 2)) { player.transform.Rotate(Vector3.up, +90f, Space.Self); } else { player.transform.Rotate(Vector3.up, -90f, Space.Self); } } } } } 

      Apparently you want to implement character management. In general, the script probably should work, only your touch variable for the controler class is not initialized anywhere. In Update you use a variable with the same name in the loop, but it ceases to exist after the program exits the loop. If you are only interested in the first touch, then you can set this.touch = touch; break; this.touch = touch; break; and further it has to be processed, but it seems to me that it is wrong, it is necessary to process every touch.

      • you're right. This control character. The character is faced with a choice (right - left), and the choice is for the player, touching the glory or right. On the next obstacle, everything repeats. That is, at the moment of choice, the first touch is of interest. And during the game all the next. Look, did I get your advice right? Did not help. if (touch.phase! = TouchPhase.Ended && touch.phase! = TouchPhase.Canceled) fingerCount ++; this.touch = touch; break; - Georgich
      • And how are you going to handle when you press with two fingers at once? Sooner or later this situation will definitely happen. Take in brackets to process all the commands in a package, and not just the first counter increased. if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {fingerCount++; this.touch = touch; break;} if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {fingerCount++; this.touch = touch; break;} - KingPeas
      • I do not understand what's the matter. My unit does not respond to touch. Or should not? Is a mouse click in the editor equivalent to touching the screen? I did not compile, checked in the editor with the mouse. - Georgich
      • thanks for the help. I apologize for the dullness and tediousness. I tried to manage in the editor with the mouse.)) Actually, the script works, but I need to finish it. Turns to the right - to the left are possible only in turns, and it may take several turns in one direction in a row. Either these are global coordinates. - Georgich