Search Unity

RaycastHit2d problems

Discussion in '2D' started by Wacky-Moose, Dec 9, 2013.

  1. Wacky-Moose

    Wacky-Moose

    Joined:
    Jun 19, 2013
    Posts:
    63
    Hello fellow unity users :)

    I have a small problem with the new 2d raycastHit

    My code
    Code (csharp):
    1.    void Update()
    2.     {
    3.         var i = 0;
    4.         while (i < Input.touchCount)
    5.         {
    6.             if (Input.GetTouch(i).phase == TouchPhase.Began)
    7.             {
    8.                 RaycastHit2D hit = Physics2D.Raycast(transform.position, Input.GetTouch(i).position, 1000);
    9.                 if (hit != null)
    10.                 {
    11.                     Destroy(hit.transform.gameObject);
    12.                 }
    13.             }
    14.             ++i;
    15.         }
    16.     }
    It seems the position on the phone where i hit is random.
    Can anybody help me with this ??

    Best regards

    Wacky Moose
     
  2. Wacky-Moose

    Wacky-Moose

    Joined:
    Jun 19, 2013
    Posts:
    63
    I have solved the problem.

    Here is my code
    Code (csharp):
    1.  
    2.     public Camera cam;
    3.     void Update()
    4.     {
    5.         var i = 0;
    6.         while (i < Input.touchCount)
    7.         {
    8.             if (Input.GetTouch(i).phase == TouchPhase.Began)
    9.             {
    10.                 RaycastHit2D hit = Physics2D.Raycast(cam.ScreenToWorldPoint(Input.GetTouch(i).position), new Vector2(0,0));
    11.                 if (hit != null)
    12.                 {
    13.                     Destroy(hit.transform.gameObject);
    14.                 }
    15.             }
    16.             ++i;
    17.         }
    18.     }
    19.  
    Best regards

    Wacky Moose