- Home /
Detect Object At on Vector2
I am spawning instantiated objects which stacked on top of each other. I want to locate the stacked object by click. I am using overlapcircleall bu t it seems like it is not working. Hope you guys can help.
My code snippet:
private Vector2 fp; // first position
private Vector2 lp; // last position
void Update () {
if (Input.GetMouseButtonDown (0)) {
fp = Input.mousePosition;
Debug.Log ("Button pressed at "+ fp );
Collider2D[] hitColliders = Physics2D.OverlapCircleAll(fp, 0, Physics2D.DefaultRaycastLayers,Mathf.Infinity,Mathf.Infinity);
foreach (Collider2D collider in hitColliders) {
Debug.Log ("Collided with: " + collider);
}
/*if (Input.GetMouseButtonUp (0)) {
lp = Input.mousePosition;
Debug.Log ("Button released at "+ lp);
}*/
}
}
Answer by incorrect · Jun 14, 2018 at 05:49 PM
As it is stated in Input.mousePosition description, it returns you mouse coordinates on the screen in pixels, not the world coordinates in units which you need for Physics2D.OverlapCircleAll.
What you need to do is to convert those screen coordinates in world space. You can use Camera.ScreenToWorldPoint method for that purpose. Just google it, there are a lot of examples around, I don't think it's necessary to explain much.
Thanks man, I made the necessary changes and it worked perfectly!
Your answer

Follow this Question
Related Questions
Punching a crate 0 Answers
Inaccurate collision detection (Bumps) 2 Answers
Slant Physics Raycast Implementation 0 Answers
Detecting a collider in 3D space. 1 Answer
Camera gets flung off of the map when the player collides with certain objects. 0 Answers