Search Unity

GUI | Touch Position Error

Discussion in 'Scripting' started by TechnoObi, Aug 29, 2014.

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    I have this script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. namespace Runner
    6. {
    7.     public class TouchScreenSide : MonoBehaviour
    8.     {
    9.  
    10.  
    11.         public Rect LinksRect;
    12.         public Rect RechtsRect;
    13.  
    14.         public bool linksb = false;
    15.         public bool rechtsb = false;
    16.  
    17.  
    18.         public Texture2D linkst;
    19.         public Texture2D rechtst;
    20.  
    21.         [HideInInspector]
    22.         public bool pressed = false;
    23.         [HideInInspector]
    24.         public bool pressedleft = false;
    25.         [HideInInspector]
    26.         public bool pressedright = false;
    27.  
    28.  
    29.         // Use this for initialization
    30.         void Start()
    31.         {
    32.  
    33.         }
    34.  
    35.         // Update is called once per frame
    36.         void Update()
    37.         {
    38.             //Links
    39.             LinksRect = new Rect(0, 0, Screen.width / 2, Screen.height);
    40.  
    41.             //Rechts
    42.             RechtsRect = new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height);
    43.  
    44.             if (linksb && rechtsb)
    45.             {
    46.                 linksb = false;
    47.                 rechtsb = false;
    48.                 Debug.Log("Entweder linksb ODER rechtsb!");
    49.  
    50.             }
    51.             GetTouchd();
    52.  
    53.         }
    54.  
    55.         void OnGUI()
    56.         {
    57.             if (linksb)
    58.             {
    59.                 GUI.DrawTexture(LinksRect, linkst);
    60.  
    61.             }
    62.  
    63.             if (rechtsb)
    64.             {
    65.                 GUI.DrawTexture(RechtsRect, rechtst);
    66.  
    67.             }
    68.  
    69.  
    70.  
    71.         }
    72.  
    73.         void GetTouchd()
    74.         {
    75.             if (linksb)
    76.             {
    77.                 bool touch1 = false;
    78.                 for (int i1 = 0; i1 < Input.touchCount; i1++)
    79.                 {
    80.                     if (LinksRect.Contains(Input.touches[i1].position))
    81.                     {
    82.                         pressed = true;
    83.                         pressedleft = true;
    84.                         touch1 = true;
    85.  
    86.                     }
    87.                 }
    88.                 if (Input.GetMouseButton(0) && LinksRect.Contains(Input.mousePosition))
    89.                 {
    90.                     pressed = true;
    91.                     pressedleft = true;
    92.                 }
    93.                 else if (!touch1) pressed = false;
    94.                 else if (!touch1) pressedleft = false;
    95.             }
    96.  
    97.             if (rechtsb)
    98.             {
    99.                 bool touch = false;
    100.                 for (int i = 0; i < Input.touchCount; i++)
    101.                 {
    102.                     if (RechtsRect.Contains(Input.touches[i].position))
    103.                     {
    104.                         pressed = true;
    105.                         pressedright = true;
    106.                         touch = true;
    107.                     }
    108.                 }
    109.                 if (Input.GetMouseButton(0) && RechtsRect.Contains(Input.mousePosition))
    110.                 {
    111.                     pressed = true;
    112.                     pressedright = true;
    113.                 }
    114.                 else if (!touch) pressed = false;
    115.                 else if (!touch) pressedright = false;
    116.  
    117.             }
    118.  
    119.  
    120.         }
    121.     }
    122. }
    123.  
    But when I press on the right side of the screen, the left and the right one are touched. Where is my error?