0

I'm developing a 2D game and I need to be able to detect the mouse position in the game world. The thing is, when I'm on the editor it works just fine, but when I run the standalone and set a resolution with an aspect ratio different from the native one on my computer, it has a weird offset.

Click offset

In the picture, the red dot represents the position of my mouse when I click, and the yellow dot represents where the game detects the position. I don't have this problem when I run the fullscreen with an aspect ratio 16:9, which is my PC's default.

The code for the detection is the following.

void useHook(Vector3 mousePosition)
{
    Vector2 mousePos = new Vector2(mousePosition.x, mousePosition.y);

    Vector2 ray2D = Camera.main.ScreenToWorldPoint(mousePos);
    RaycastHit2D hit = Physics2D.Raycast(ray2D, Vector2.zero);
    if (hit.collider != null)
    {
        GameObject target = hit.collider.gameObject;
        if (target.tag.Equals("Grappleable"))
        {
            //Grapple
        }
    }
}

I'm using an ortho camera and calling useHook like this.

Update()
{
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        useHook(Input.mousePosition);
    }
}
RJD
  • 48
  • 1
  • 7
  • Are you using an orthographic camera? How are you calling `useHook`? – Sam Jun 26 '15 at 07:19
  • @Sam , I edited the question to be more specific. Thank you. – RJD Jun 26 '15 at 15:16
  • Hm well I set up a test project and used your code, and it works fine in Unity 4.6.1f1. How are you calculating that position for the yellow circle? – Sam Jun 28 '15 at 05:15
  • @Sam , I built it with 4.6, full screen and everything, it works. Thank you. It must be a problem with the new Unity Version (5.1.1f1) – RJD Jun 29 '15 at 00:44
  • Ahh yeah, I'm also seeing that behaviour in 5.1.1f1. I submitted a bug report. I couldn't see any existing bugs. – Sam Jun 29 '15 at 04:00

0 Answers0