Hello Guest

Author Topic: Positioning Custom Mouse Cursor  (Read 3548 times)

Vartib

  • Newbie
  • *
  • Posts: 21
    • View Profile
Positioning Custom Mouse Cursor
« on: December 06, 2013, 10:26:34 pm »
I'm trying to create a custom mouse cursor for my game using sprites. I have two cameras set up, one to display the game world and one to display UI elements; the cursor will be displayed by the second camera. That said, I'm not sure how to get the mouse's position relative to the UI camera. Any help is appreciated :)

EDIT: To clarify, Input.mousePosition won't work as that gives the position relative to the screen rather than the camera. In the image linked below the mouse cursor in the middle of the screen above the other menu is at (0, 1.5). 0 for the x position is in the very middle of the screen, but 0 for y is off the screen toward the bottom.

http://i.imgur.com/Yt1qq2h.jpg
« Last Edit: December 06, 2013, 10:42:44 pm by Vartib »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Positioning Custom Mouse Cursor
« Reply #1 on: December 06, 2013, 10:59:12 pm »
camera.ScreenToWorldPoint will give you a coordinate in world space. You can convert Input.mousePosition using this.

Vartib

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Positioning Custom Mouse Cursor
« Reply #2 on: December 06, 2013, 11:29:04 pm »
Thanks for the fast reply, works perfectly! In case anyone needs this in the future here's what I have:

Code: [Select]
public Camera guiCamera;
public Vector2 screenPosition;

screenPosition = guiCamera.ScreenToWorldPoint (Input.mousePosition);

Make sure to drag the camera you're using to display the GUI into guiCamera in the editor.