playMaker

Author Topic: Lock the mouse to the screen, but allow movement within it?  (Read 730 times)

westingtyler

  • Sr. Member
  • ****
  • Posts: 274
    • View Profile
    • My Video Game Projects Web Site
Lock the mouse to the screen, but allow movement within it?
« on: March 11, 2020, 02:49:39 PM »
Hello. My menus use the Unity UI Canvas and event system. in order to use the mouse in this system, I must UNLOCK the mouse upon entering the menu. I have the mouse locked while walking around in 3d 3rd person mode to move the camera, this way the mouse doesn't fly out of the game space.

But if the mouse is locked while in the menus, the visible cursor sticks to the center of the screen and cannot move around.

So therefore I must unlock the mouse upon entering menus, so the player can use the menu with their mouse But this means their mouse can now fly out of the game screen, only while in menus.

Is there some way to prevent the mouse from leaving the game screen, but allowing the pointer to move around freely within  it?

Pro games do this all the time. Resident Evil 2 remake, for example, allows you to move the mouse all around your inventory, but it still will not go outside the edge of the game screen while you are in those inventory menus.

Surely there is a way to do this? Does this need a special action or something?

rechronicle

  • Full Member
  • ***
  • Posts: 119
  • Mystvaldia
    • View Profile
    • Indie RPG Creator
Re: Lock the mouse to the screen, but allow movement within it?
« Reply #1 on: March 12, 2020, 10:55:12 AM »
How about getting the game screen size and clamp down mouse movement in there while a certain menu is active?

westingtyler

  • Sr. Member
  • ****
  • Posts: 274
    • View Profile
    • My Video Game Projects Web Site
Re: Lock the mouse to the screen, but allow movement within it?
« Reply #2 on: March 17, 2020, 01:03:39 AM »
the problem is, if the mouse is not locked to the game window, I can't clamp the mouse movement to prevent it from leaving the screen. it functions outside of any playmaker actions or the game itself, like a windows-os-level input. but if the mouse IS locked to the game screen, it stays in the center and does not move around the screen at all.

I have even tried creating a fake mouse pointer image on the canvas, somehow move it around the screen based on mouse input... but that only works if the mouse is not locked. if the mouse is locked, I can't move the mouse around onscreen to get its location OR get its movement delta floats.

but surely there's a better way than faking a mouse pointer on screen and feeding real mouse movement into it?

I tried a "Set Mouse Confined" action, but this seems to just lock it. I was hoping "confined" meant it could still move within the screen, but no, confined and locked seem to do the same thing - freeze the mouse pointer in the center of the game window.

I tried the Get Mouse Movement Delta action, which DOES store the actual movement amounts if I move the mouse... BUT, only if the mouse is NOT locked, which defeats the whole purpose. I can't for the life of me figure this out, how to make the mouse move on the game window while not moving outside it. It should be so simple.
« Last Edit: March 17, 2020, 02:47:49 AM by westingtyler »