Hello there.
I’m trying to use Physics2D.Raycast to check if user hit the game object. I try to use code i found in the forum:
void Update()
{
if ( Input.GetMouseButtonDown( 0 ) )
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint( Input.mousePosition );
RaycastHit2D hit = Physics2D.Raycast( worldPoint, Vector2.zero );
if ( hit.collider != null )
{
Debug.Log( hit.collider.name );
}
}
}
So i create new Game object of type Sprite (2D object), add C# with code above. But hits not working. It works only if i add “Box collider 2D” to the game object.
I’m not sure if i there must be Box collider or not, becouse in every example there is not any collider used.
Sorry for question but I’m newbie in unity and i’m stuck in this for whole weekend.
Thank you.