Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

How to use Ray2d and Raycasthit2d..

Discussion in 'Scripting' started by DalerHakimov, Apr 11, 2014.

  1. DalerHakimov

    DalerHakimov

    Joined:
    Mar 14, 2014
    Posts:
    302
    Hey,

    I have few sprites on the screen and when I click with mouse on them, I want to detect which one has been clicked. I've found a simple script that does it:
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     if( Input.GetMouseButtonDown(0) )
    5.     {
    6.        Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition ); //this one here ScreenPointToRay is Vector3, actual problem
    7.        RaycastHit hit;
    8.  
    9.        if( Physics.Raycast( ray, out hit ) )
    10.        {
    11.             Debug.Log( hit.transform.gameObject.name );
    12.         }
    13.     }
    14. }
    15.  
    Code above works using simple Ray. As I'm about to do 2D game, I want to use all 2D optimized functions and etc.. Can someone explain, is that possible to detect a hit via 2D functions from mouse click on that particular object?
     
  2. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.     if ( Input.GetMouseButtonDown( 0 ) )
    5.     {
    6.         Vector2 worldPoint = Camera.main.ScreenToWorldPoint( Input.mousePosition );
    7.         RaycastHit2D hit = Physics2D.Raycast( worldPoint, Vector2.zero );
    8.         if ( hit.collider != null )
    9.         {
    10.             Debug.Log( hit.collider.name );
    11.         }
    12.     }
    13. }
    14.  
     
  3. DalerHakimov

    DalerHakimov

    Joined:
    Mar 14, 2014
    Posts:
    302
    Thank Bivrost,

    I have this script attached in 3 gameobjects in the scene. So, whenever I click one of them, it gives output 3 times. Do I need to name them differently and check whether this object with some id has been clicked, or there are better ways?

    This is your second time helping me) I owe you a beer))
     
    Alooa and Gawron10001 like this.
  4. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    Make sure that the script runs only once per frame. Attach it to an empty game object, for example.
     
  5. DalerHakimov

    DalerHakimov

    Joined:
    Mar 14, 2014
    Posts:
    302
    Amazing.. it works. Great thanks)
     
  6. jackdracon

    jackdracon

    Joined:
    May 7, 2011
    Posts:
    11
    Hi everybody,
    I upgraded my Unity to the last version (4.5), but I am get some problems, mainly with collision.
    I tried the code above for detection of Sprites on the screen, but don't worked, as the code I did for the previous version (that had worked) in Unity3D.
    Today, my only way to "fix" that, is using the event "OnMouseDown" on my object.
    Somebody have the same problem? Or how to fix in another way?
    []'s
     
  7. cgkaransahu

    cgkaransahu

    Joined:
    Jan 8, 2020
    Posts:
    5
    Code (CSharp):
    1. Vector2 ray = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    2.             RaycastHit2D _hitInfo = Physics2D.Raycast(ray, Vector2.zero);
    3.             if (_hitInfo.collider != null)
    4.             {
    5.                 Destroy(_hitInfo.transform.gameObject);
    6.             }
     
    frenchfries5 likes this.
unityunity