Search Unity

How to make the transparent parts of a sprite also clickable using a Raycast

Discussion in 'Scripting' started by nowletsstart, Sep 22, 2017.

  1. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    I have been making using the following code to add mouse clicks to my game objects.

    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    2.     if ((hit.collider != null && hit.collider.gameObject.name == gameObject.name)
    3.     )
    4.     {
    5.  
    6.     //do something here
    7. }
    I have a game object which I made using a sprite with transparent areas. I would like the transparent areas to also be recognized as the game object being clicked. I am not sure if this can be achieved using a raycast. If not, I am open to other ways of achieving the same result.
     
    cdr9042 likes this.
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408