Search Unity

OnMouseDown() touch emulator working but...

Discussion in 'iOS and tvOS' started by VonderVizion, Nov 21, 2010.

  1. VonderVizion

    VonderVizion

    Joined:
    Aug 30, 2010
    Posts:
    8
    Technicat posted this code a while back.



    Code (csharp):
    1. function Update () {
    2.  
    3.     var hit : RaycastHit;
    4.  
    5.     for (var i = 0; i < Input.touchCount; ++i) {
    6.  
    7.         if (Input.GetTouch(i).phase == TouchPhase.Began) {
    8.  
    9.         // Construct a ray from the current touch coordinates
    10.  
    11.         var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
    12.  
    13.         if (Physics.Raycast (ray,hit,1000)) {
    14.  
    15.             hit.transform.gameObject.SendMessage("OnMouseDown");
    16.  
    17.         }
    18.  
    19.     }
    20.  
    21.     }
    22.  
    23. }
    It works great, but I need OnMouseUp() to work as well. I've tried and failed.
     
  2. VonderVizion

    VonderVizion

    Joined:
    Aug 30, 2010
    Posts:
    8
    It took me way too long to figure this out.

    Code (csharp):
    1. private var thisTransform : Transform;
    2.  
    3. var tmpobj : Transform;
    4.  
    5.  
    6.  
    7. function Start()
    8.  
    9. {
    10.  
    11.     thisTransform = transform;
    12.  
    13. }
    14.  
    15. function Update ()
    16.  
    17. {
    18.  
    19.  
    20.  
    21.     var hit : RaycastHit;
    22.  
    23.     for (var i = 0; i < Input.touchCount; ++i)
    24.  
    25.     {
    26.  
    27.         if (Input.GetTouch(i).phase == TouchPhase.Began)
    28.  
    29.         {
    30.  
    31.             // Construct a ray from the current touch coordinates
    32.  
    33.             var ray = camera.ScreenPointToRay (Input.GetTouch(i).position);
    34.  
    35.             if (Physics.Raycast (ray,hit,1000))
    36.  
    37.             {
    38.  
    39.             hit.transform.gameObject.SendMessage("OnMouseDown");
    40.  
    41.             tmpobj = hit.transform;
    42.  
    43.             }
    44.  
    45.         }
    46.  
    47.         else if(Input.GetTouch(i).phase == TouchPhase.Ended)
    48.  
    49.         {
    50.  
    51.         tmpobj.SendMessage("OnMouseUp");       
    52.  
    53.         }
    54.  
    55.     }
    56.  
    57.    
    58.  
    59. }
    60.  
    61.