0
\$\begingroup\$

I have 3D world. I want to detect when the player clicks on a game object in the world.

this image

In this image, I have 2 objects I want to detect mouse click on. The white sphere does have collider on it. The dumpster does not. The behavior is the same as noted below.

I am getting the mouse down detection. When I check the RaycastHit, I always get the player game object transform and not the game object I clicked on in the world. If I remove the raycaster checks, then I get mousedown for anything in the game not just mouse down over the game object. I need to be able to distinguish between clicks on different game objects.

I found a post here and added a PhysicsRayCaster, but it didn't change the behavior. The problem isn't getting the event. My problem is detecting which game object the player clicked on in the world.

Here's my code:

public class InventoryItemSearchHandler : MonoBehaviour
{     
    private void Update()
    { 
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                // CompareTo always fails because hit.transform.name is always "Player" and
                // not the name of the object clicked on
                if (0 == hit.transform.name.CompareTo(transform.name))
                {
                    print("mouse down");
                }
            }
        }
    }
}

EDIT: In the image below (compared to the one above) I do get the correct transform in the RaycastHit type when I click on the white sphere (and when I click on the dumpster I get the player transform as noted above).

enter image description here So it looks like the player object is getting in the way. What would be causing that?

\$\endgroup\$
5
  • \$\begingroup\$ Try showing us the collider(s) on your player object. \$\endgroup\$
    – DMGregory
    Jan 19, 2020 at 12:46
  • \$\begingroup\$ @DMGregory is there way to see the collider boundaries at run time? \$\endgroup\$
    – tatmanblue
    Jan 19, 2020 at 23:37
  • \$\begingroup\$ Yes, you'll find it in the Gizmos menu of your Game window. \$\endgroup\$
    – DMGregory
    Jan 19, 2020 at 23:42
  • \$\begingroup\$ yeah so I think that was it---box collider was getting in the way. Once I changed the sizes of the box collider then click detection worked the way I expected/wanted. Thank you for helping me find that. \$\endgroup\$
    – tatmanblue
    Jan 20, 2020 at 14:37
  • 1
    \$\begingroup\$ Please post your solution as an Answer — it might help others! Showing a screenshot of how you diagnosed the problem could be particularly useful. \$\endgroup\$
    – DMGregory
    Jan 20, 2020 at 16:15

1 Answer 1

1
\$\begingroup\$

Turns out, the problem was the boundaries of the box collider attached to the player. As shown in the image below, box collider boundaries (green lines) covered the game objects I thought I was clicking on and that blocked getting the click messages. enter image description here

Once I changed the collider boundaries, the click messages were getting through to the objects.

The collider boundaries can be turned on by clicking the gizmos button in the game window.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .