Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Problem with BoxCollider2D detecting the mouse

Discussion in '2D' started by valentincerda19, Aug 21, 2023.

  1. valentincerda19

    valentincerda19

    Joined:
    Jul 19, 2021
    Posts:
    2
    I have placed the camera at (0,0), and when I open the inventory, the camera moves to (1000, 1000), and I instantiate an object there. The object has a BoxCollider2D and a script that prints a message to the console when you place the mouse over the object. However, at a random moment, the BoxCollider2D doesn't detect the mouse, but collisions with other objects work. I have checked the script, and there isn't a problem there. What can I do? Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    BoxCollider2Ds out of the box would definitely NEVER detect a mouse. The mouse and the 2d physics system are completely unrelated.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.
     
  3. valentincerda19

    valentincerda19

    Joined:
    Jul 19, 2021
    Posts:
    2
    Sorry, I made a mistake regarding the tag, but I don't understand what you mean by 'BoxCollider2Ds out of the box.' What I'm saying is that I have a BoxCollider2D on an object that consistently registers collisions, but it has issues with mouse interactions. Most of the time, it detects both the OnMouseEnter() event and a raycast sended from the mouse. However, I encounter a problem: there are times when it fails to register these interactions. The issue is resolved by disabling and then re-enabling the component, but I have no way of knowing when it won't work other than through the feedback I just mentioned. Overall, it seems strange and unintended that this occurs.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Overall it seems strange and unintended that you would think we could read your scripts from here.

    When you say this:

    Then read my post above.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    OnMouseEnter is nothing to do with 2D and the physics system doesn't perform this work; it's a feature of the Camera and its using physics queries in the same way you would to produce these calls. It's impossible to help you from a description though.

    I don't know what you mean by "raycast sended from the mouse".

    Internally OnMouseXXX is using GetRayIntersectionNonAlloc which is a pseudo 3D raycast but it'd be better using the more appropriate OverlapPoint. Personally, I wouldn't rely on those calls and use the overlap-point to ask what's the mouse cursor is overlapping.