• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tritize · Aug 20, 2020 at 02:59 PM · uicanvascursormouseposition

UI and mousePosition not centered the same way ?

Hi,

I have a problem with some UI elements which are on a canvas (which scale with screen size). The ancors of the elements are at the center of the screen. However, when I check the position of the mouse on the screen (with a Debug.Log() in Update(), and I'm using the function "Input.mousePosition"), I can see that the elements are not centered on the (0, 0) of the mouse position ! For the cursor, the default cursor works fine, but as soon as I use a custom one the same difference arise. I've tested multiple screen size, editor or build, and it's always the same gap between the (0, 0) of the mousePosition and the UI / customCursor.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by The_Three_Vs · Aug 20, 2020 at 07:22 PM

The (0, 0) for the mouse position is at the bottom-left of the screen, while the (0, 0) of the center-anchored UI elements will be in the center of the canvas/screen. You'll need to manually adjust for this in your code. For example, if you want your mouse position to act like its (0, 0) is in the center of the screen, you will need to do the following:

 //The mouse position based on the mouse's coordinate system:
 float naturalMouseX = Input.mousePosition.x;
 float naturalMouseY = Input.mousePosition.y;
 
 //The mouse position based on the canvas/screen's coordinate system:
 float screenMouseX = Input.mousePosition.x - (Screen.width / 2);
 float screenMouseY = Input.mousePosition.y - (Screen.height / 2);

Hope this helps!

EDIT:

1: The click point of any cursor is at the top-left corner of the texture being used. To fix this, you'll have to modify the 'hotspot' - the click point - of the cursor when you use Cursor.SetCursor. To position it in the center of the custom cursor, you can use:

  public Texture2D texture;
 
  private void Start()
  {
      Cursor.SetCursor(texture, new Vector2(texture.width / 2, texture.height / 2), CursorMode.Auto);
  }

2: Screen.width, Texture.width, etc., are int values. This means that when dividing them by 2, the result will be different then expected (due to int division not allowing decimals). To fix this, add (float) before Screen.width and other related int values.

That said, I don't know how much of an impact this will have, as in the case of dividing by two, there will only be a difference of 0.5 in each case.

One more thing that might help explain things. The screen is displayed in pixels, which cannot display two things at once. Therefore, if there is something that is an even number of pixels in size, it will either be cropped or shifted to an adjacent pixel, meaning it's center will be about half a pixel away from what you expect.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tritize · Aug 21, 2020 at 06:55 AM 0
Share

I didn't gave enough details my bad, but I'm already using the exact same formula as you did to have the 0,0 of the mouse centered. However, when I put the cursor on the center of the ui it's not (0,0) (it's more like (-4x, -1y)) and so when I reach (0,0) with the mouse it's not centered with the ui.

avatar image The_Three_Vs Tritize · Aug 21, 2020 at 04:57 PM 0
Share

What sort of custom mouse pointer are you using? Is the 'click point' supposed to be in the upper-left, like the default mouse, or is it in the middle, like a target reticle?

avatar image Tritize The_Three_Vs · Aug 23, 2020 at 05:20 PM 0
Share

It is in the middle like a target reticle, do you think it can have something to do with it ?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

233 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hololens and UI Canvas, interactive zone inconsistent with actual UI elements 1 Answer

Moving an in-game cursor using the player's current mouse position? 0 Answers

Ui image appear and disappear on trigger 1 Answer

Canvas, Scroll Rect, Panels, Panes? 0 Answers

Two panels to fit the size of the parent panel in Portret mode. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges