- Home /
2D click on object with a raycast not working
Hello all,I need to check the click on a 2D gameobject. I've tried this:
if (Input.GetMouseButtonDown(0))
{
Ray2D ray = new Ray2D(Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y)), Vector2.zero);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit.collider!=null)
{
Debug.Log(hit.collider.gameObject.name);
}
}
but it doesn't work... So I tried to pass a 3d ray on a Physics2D.Raycast
if (Input.GetMouseButtonDown(0))
{
Ray b=Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(b.origin, b.direction);
if (hit.collider!=null)
{
Debug.Log(hit.collider.gameObject.name);
}
}
But it doesn't work too,I've also tried to manually set a distance and a direction in the ray's declaration and in the raycast's physics2d.raycast method but it didn't change nothing. This piece of code is in the Update method but I tried it also on the FixedUpdate but it was the same thing. The only time when it detected some colliders is when i set the direction to -vector2.up but it detects only the terrain gameobjects.I tried to hack it by setting the origin to a higher point and reducing the distance but it didn't work well,it doesn't detect all the colliders. I searched on the internet almost all the arguments about detecting a click in a 2d context,I found the OnMouseDown() method but I prefer using the raycasting,to learn it. I've also seen that a lot of people use these 2 ways to detect a click but it doesn't work to me. What's wrong? Thank you in advance for your help :D
Answer by Danisuper · Apr 13, 2016 at 07:02 PM
Found a solution to check what object I clicked on,I used Physics2D.OverlapPoint() here the complete code:
GameObject ClickedGameobject()
{
GameObject clicked;
Collider2D clicked_collider = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition));
clicked = clicked_collider.gameObject;
return clicked;
}
Answer by aditya · Apr 13, 2016 at 05:18 AM
why are you using COMPLICATED rays instead of SIMPLE SpriteRenderer.bounds.Contains
you should add this as comment ins$$anonymous$$d as a Answer.
it may be a good input,I tried working around on it,but the problem now is how do I know wich object belongs the spriterenderer?
GameObject go;
SpriteRenderer go_renderer=go.GetComponent<SpriteRenderer>();
Vector3 mouse_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if(go_renderer.bounds.Contains(mouse_pos))
{
//do stuff
}
I mean,how to assign go? I must check if I click on a monster/object,so this code isn't going to be on the object with the spriterenderer in the code that check if the mouse in in bounds,that is what is blocking me now,a solution may be putting all gameobjects with a tag in an array and foreach object in the array checking the bounds but I think it would be very inefficent because in the game there will be a lot of calls to this. I think raycasts are the best solution but looks like the code in my question isn't working lol
just make go_renderer
no go
needed and simply drag and drop the gameObject which have your sprite on it and the script will get your spriterenderer, just make the variable public, just make sure that go_renderer
is of type Spriterenderer