I'm working on 2019.4.9f1 on a 2D project using Universal Rendering Pipeline. The camera settings changed so I'm stating it.

I wrote a OnMouseEnter and OnMouseExit in a script to manage whenever my mouse cursor is on top of a SpriteRenderer with a collider located at 0,0,0. I have a main camera located at 0,0,-10. I've tested the script that manages the OnMouse events. It works fine.

Problem: But the moment I add a new camera (UI Camera set to Overlay located at 0,0,-10) in and it is enabled, OnMouse events aren't getting called. Disabling the UI camera makes the OnMouse events work normally.

Solutions that have failed:

  1. I've tried setting the tag of the UI Camera to MainCamera and set Main Camera's tag to Default. https://answers.unity.com/questions/241334/onmousedown-w-multiple-cameras-doesnt-work-right.html
  2. I tried making the UI Camera a bit closer to the sprites. https://answers.unity.com/questions/425478/onmousedown-not-firing.html

I intend to avoid Raycasting and get the OnMouse Events to work because I want to fix the problem and not work around it.

Question: Any possible reason why my OnMouse events aren't called?

  • \$\begingroup\$ "I intend to avoid Raycasting" I have some bad news for you about how the OnMouse events work then. They're still raycasting under the hood, they're just using the most inefficient raycast possible because the engine can't guess what you want and has to cover the widest possible set of use cases. Removing your OnMouse events and firing your own ray will allow you to get finer control over how the events are called and the performance of the testing. \$\endgroup\$ – DMGregory Sep 30 '20 at 19:45
  • \$\begingroup\$ I see. I guess it can't be helped. Though, how can I reliably check for a 2D object that is directly below my cursor. I've checked answers.unity.com/questions/596792/… and it says to add a 3D collider for a raycast to work. is this necessary or is there a better way of doing it? \$\endgroup\$ – CraftedGaming Oct 1 '20 at 0:32
  • 1
    \$\begingroup\$ Presumably you considered Camera.ScreenToWorldPoint and Physics2D.OverlapPoint? \$\endgroup\$ – DMGregory Oct 1 '20 at 0:36
  • \$\begingroup\$ Oh. Just tested ScreenToWorldPoint while I scrolled through the link. this works. didn't know they existed. \$\endgroup\$ – CraftedGaming Oct 1 '20 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.