Search Unity

Hittest | Left or Right Screenside

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

  1. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    I have this two rects:
    Code (csharp):
    1.  
    2. LeftRect = new Rect(0, 0, Screen.width / 2, Screen.height);
    3. RightRect = new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height);
    4.  
    The "LeftRect" creates an image on the left side of the screen and "RightRect" creates an image on the right side of the screen. And how can I get the information, that one of that is touched on my Android device? I don't know, how to use "Hittest".
     
  2. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Code (CSharp):
    1. foreach (Touch touch in Input.touches)
    2.         {
    3.             if (touch.phase == TouchPhase.Began)
    4.             {
    5.                 Ray ray = Camera.main.ScreenPointToRay (touch.position);
    6.                 RaycastHit hit = new RaycastHit();
    7.                 if (Physics.Raycast (ray,out hit,1000.0f))
    8.                 {
    9.                     if(hit.collider.gameObject == this.gameObject)
    10.                     {
    11.                         Debug.LogWarning(hit.transform.name);
    12.                        
    13.                     }
    14.                 }
    15.             }
    16.         }
    don´t forget to assign a collider to your gameobject. Also you need a Camera which has the tag "MainCamera".
     
  3. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    This checks if I touch a collider and not the image?
     
  4. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    If u want to use GUITexture then use
    Code (CSharp):
    1. void OnMouseUpAsButton()
    2. {
    3.    //ur code
    4. }
     
  5. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    Does this also work at Android touchscreens?
     
  6. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    create guitexture and add the script.Its works fine.
     
  7. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    I created this Texture:
    Code (csharp):
    1.  
    2. GUI.DrawTexture(LeftRect, links);
    3.  
    And how can I use ith with
    void OnMouseUpAsButton()
    ?
     
  8. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    U need to create GUITexture gameobject in hierarchy.Then add the script for that gameobject.
     
  9. TechnoObi

    TechnoObi

    Joined:
    Nov 30, 2013
    Posts:
    82
    But I have created the GUI with a script and not in the hierarchy.