Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

2D Colliders Not Working With Physics2D.RaycastAll()

Discussion in 'Physics' started by DigitalGrunt, Jan 28, 2015.

  1. DigitalGrunt

    DigitalGrunt

    Joined:
    Feb 25, 2014
    Posts:
    8
    When running the sample scene provided, it works fine in Unity Web Player v4.3.4 but since updating to v4.6.1, the raycasting does not work as expected. Mouse Down/Up events still work but when you perform a raycast on 2D colliders where the transform scale is equal to or less than (0.5,0.5), they are no longer captured by the Physics2D.RaycastAll() method.

    In the sample provided, the larger 2D object can be clicked on using either method, but the smaller 2D object only responds to the Mouse down/up callbacks.

    sample.png

    Here's the simple script I put together to test this:

    Code (CSharp):
    1. public class Pickable : MonoBehaviour
    2. {
    3.     private static GameObject s_oGuiCallback = null;
    4.     private static GameObject s_oGuiRaycast = null;
    5.  
    6.     private bool m_bMouseWasDown = false;
    7.  
    8.     void OnMouseDown()
    9.     {
    10.         m_bMouseWasDown = true;
    11.     }
    12.  
    13.     void OnMouseUp()
    14.     {
    15.         if( m_bMouseWasDown )
    16.         {
    17.             PickedByCallback( name );
    18.             m_bMouseWasDown = false;
    19.         }
    20.     }
    21.  
    22.     void PickedByCallback( string sName )
    23.     {
    24.         if( s_oGuiCallback == null )
    25.         {
    26.             s_oGuiCallback = GameObject.Find( "PickGuiCallback" );
    27.         }
    28.  
    29.         s_oGuiCallback.guiText.text = String.Format( "Picked by callback: {0}", sName );
    30.     }
    31.  
    32.     void PickedByRaycast( string sName )
    33.     {
    34.         if( s_oGuiRaycast == null )
    35.         {
    36.             s_oGuiRaycast = GameObject.Find( "PickGuiRaycast" );
    37.         }
    38.  
    39.         s_oGuiRaycast.guiText.text = String.Format( "Picked by raycast: {0}", sName );
    40.     }
    41.  
    42.     void Update()
    43.     {
    44.         if( Input.GetMouseButtonDown( 0 ) )
    45.         {
    46.             RaycastHit2D[] aHits = Physics2D.RaycastAll( Camera.main.ScreenToViewportPoint( Input.mousePosition ), Vector2.zero );
    47.  
    48.             if( aHits.Length > 1 )
    49.             {
    50.                 List<RaycastHit2D> oHits = new List<RaycastHit2D>( aHits );
    51.  
    52.                 // Sort list and get the closest GUI object to the camera
    53.                 oHits.Sort( Ascending );
    54.  
    55.                 PickedByRaycast( oHits[ 0 ].transform.name );
    56.             }
    57.             else if( aHits.Length > 0 )
    58.             {
    59.                 // Only one GUI object to choose from
    60.                 PickedByRaycast( aHits[ 0 ].transform.name );
    61.             }
    62.         }
    63.     }
    64.  
    65.     /// <summary>
    66.     /// Sorts a list of raycast hits by their Z position value
    67.     /// </summary>
    68.     /// <param name="a"></param>
    69.     /// <param name="b"></param>
    70.     /// <returns></returns>
    71.     private int Ascending( RaycastHit2D a, RaycastHit2D b )
    72.     {
    73.         if( a.transform.position.z > b.transform.position.z ) return 1;
    74.         if( a.transform.position.z < a.transform.position.z ) return -1;
    75.         return 0;
    76.     }
    77. }
    Has anyone else run into this problem???
     

    Attached Files:

    Last edited: Jan 28, 2015
  2. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    Here's your problem:

    TooSmall.png

    You may wanna scale the texture rather than the whole object.
     
    DigitalGrunt likes this.
  3. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    The site mysteriously won't lemme post 2 pictures in one go but you should aim for something along these lines...

    SomethingALongTheseLInes.png
     
    DigitalGrunt likes this.
  4. DigitalGrunt

    DigitalGrunt

    Joined:
    Feb 25, 2014
    Posts:
    8
    Thanks for the feedback. We're currently using version 4.5 of unity so we don't see those error messages in the inspector. Interestingly enough, this wasn't a problem in the previous versions - I wonder why they restricted it. Nevertheless, I'll see if we can compensate using your suggestion. Thanks again! :)

    Cheers,
    G.