Search Unity

Touch exit

Discussion in 'Scripting' started by gk104, Jul 29, 2014.

  1. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57
    I have a code that when i touch a cube it changes its color, but the problem is that i dont want it to change color when my finger still on the screen..

    please see the code below... my purpose is to make the boolean that called "touched" to false when my finger leaves the screen/not touching..

    Code (CSharp):
    1.        
    2.         for(int i = 0; i < Input.touchCount; i++)
    3.         {
    4.             Touch touch = Input.GetTouch(i); // touch input
    5.             Ray touchRay = Camera.main.ScreenPointToRay(touch.position); // touch position
    6.             RaycastHit[] touchHits = Physics.RaycastAll(touchRay); // touch array
    7.  
    8.                   for (int n = 0; n < touchHits.Length; n++)
    9.                   {
    10.                      if (touchHits[n].collider.name == "Cube" && !touched) // if i touch the cube and the bool 'touched' is false
    11.                      {
    12.                       Color newColor = new Color( Random.value, Random.value, Random.value, 10f );
    13.                       renderer.material.color = newColor; // changes color
    14.  
    15.                      touched = true; // here it stops changing the color after i touched
    16.                                      // one time and my finger is still on the screen
    17.                      }
    18.  
    19.                   }
    20.         }
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  3. gk104

    gk104

    Joined:
    Apr 25, 2014
    Posts:
    57