MousePosition Confusion

So I was using what I learned in this course to make a grid.
When I click a card it loads a ghost that hovers over gridposition closest to the mouse.
When I click a position with a ghost it instantiates the prefab.

Now I was having a weird bug where despite hovering the correct location on a tile, my mouse was occasionally hovering the next tile over or even diagonally (up and / or right respectively).

In my memory, there was something I remember learning about offsetting the mouseposition by a certain vector to get the true position, this is not in the course so I couldn’t remember where I learned it and can’t find any reference to it online with my limited searching skills.

I subtracted the vector3 (1,1) from my Vector2 GetPosition() results and immediately it fixed the weird mouse issue.

return Camera.main.ScreenToWorldPoint(InputManager.Instance.GetMouseScreenPosition()) - new Vector3(1,1);

The question: Why???

Sounds like you probably have an offset in either your Grid conversion logic or in your Grid visual. You really should find the underlying issue instead of adding an offset to the mouse position.

Rightttttt, now it’s coming back.

That was from your tutorials where we put the pivot at zero and offset the image / building itself…

Applying an offset to the child visual is perfectly fine since it’s just a visual. So you can have the grid origin be on the lower left corner instead of the center of the grid position and then position the visual.
That’s possibly better when you have objects that occupy more than one grid position.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms