Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

RaycastAll for 2D

Discussion in 'Scripting' started by pKallv, May 2, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I am trying to do a RaycastAll for 2D and collide with all sprites that are positioned on top of each other. I have a bit of a problem to get this to work properly and do not fully understand how to put a direction in the 2D-space in this scenario.

    I would like to ask for some help how the full statement will look like?

    Here is the syntax:
    Code (csharp):
    1. static RaycastHit2D[] RaycastAll(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);
    Here is the code i use to select a single sprite:
    Code (csharp):
    1. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    Here is the code i testing:
    Code (csharp):
    1. RaycastHit2D[] hits = Physics2D.RaycastAll(hit.transform.position,-hit.transform.up,0.6f);
    :confused:
     
    Last edited: May 2, 2014
  2. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Hmmm as soon as I posted this I was able to solve it, should have tested a bit more. Here is the code i use for the test:

    Code (csharp):
    1. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    2.  
    3.             RaycastHit2D[] hits = Physics2D.RaycastAll(hit.transform.position,-hit.transform.up,0.6f);
    4.  
    5.             print ("xx: " + hits.Length);
    6.  
    7.             for(int x = 0; x < hits.Length; x++) {
    8.                 print ("S: " + hits[x].transform.tag);
    9.             }