Search Unity

Luring with cheese... Is there way to to move the mouse?

Discussion in 'Scripting' started by tsphillips, Jan 31, 2006.

  1. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Input.mousePosition is a temporary variable. Does anyone know if there is a way to otherwise position the mouse pointer?

    If standard scripting does not allow positioning the mouse pointer, then I see two other options being a plugin or requiring full-screen mode and constructing a fake mouse pointer. (Turn off system mouse pointer, etc.) The plugin route seems like a real gamble, though, as I'm not sure the same plugin would work across multiple OS versions.

    Requiring full screen is not that bad, but I haven't been able to get the player to test in full screen mode. Is there a way to force this within the Unity development environment? That is, is there a way to make the development player run in true full screen mode?

    Tom
     
  2. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    I am pretty sure in player setting where you set the standard dimentions, you can also set fullscreen for default. I dont think this will help or you already know, pressing space while the mouse is in the playerview will bring it full screen, not sure that helps.

    Bill
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Please elaborate on what you mean by 'development player'. Playing from within the editor? Sadly, no. A Built standalon game, just select it from startup options or hit CMD-F while running the game

    On a bit higher level, what are you trying to do?
     
  4. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    I have set the preferences to "Default is Full Screen" and used the space bar, but it only maximizes the view; it does not switch into full screen mode within the IDE. So, the mouse cursor never hides (Screen.showCursor = false) and the system mouse pointer keeps running over other things like the dock and the menu bar.

    [Edit: Just saw your post, Nicholas, after writing the above. Yes, I mean from within the editor. Everything works fine running a build.]

    Tom
     
  5. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    I'm trying to settle on a user-friendly way to navigate in 3-space. Some options include:


    • 1) Rotate the character with the mouse. (Like a mouse look.)
      2) Click screen, then rotate character to face the click point.
      3) Draw a circle near the center of the screen, then have the mouse cursor stay within that circle. The farther the mouse cursor is from the center, the faster the rotation.
      4) Draw a circle near the center, have the mouse position (within the circle) only after a mouse move, then have it gravitate toward the center while mouse is still.

    (Keyboard navigation is also an option.)

    It's kind of hard for me to clearly describe all these options. The most important thing is that the navigation needs to be friendly, even to casual gamers. And it is all complicated by actions, some of which depend on the direction the character is facing.

    Tom

    [Edit: Changed "cursor navigation" to "keyboard navigation. Really tired tonight.]
     
  6. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Kinda hard to help on the player controll if we dont know what type of game it is. :wink: fps, rpg, rts, sounds like it is a rts type game..

    Bill
     
  7. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Closest to a fps. For the control I need to either directly manipulate the system mouse pointer (which does not seem probable) or build a custom one for full screen mode. I don't have a problem writing a custom one, but it makes development awkward unless I can get full screen mode to come up from the editor.

    Tom
     
  8. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    I've been told "No go" from the OTEE team on directly manipulating the actual cursor position at runtime, but Delta should solve everything you're talking about... have a different GUI texture for the mouse icon correspond to actual Delta mouse movement... start the icon out at center screen if that's the crosshair or something and let Delta mouse movement control your relative position (no matter where the cursor started out from it will move correctly)... and Delta should work full screen or not.

    The question is very ambiguous, but there's my 2 cents.
     
  9. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Yep, it looks like I will go with reading mouse deltas. Still hard to test from the editor (as my system mouse clobbers my dock, menu bar, etc.), but workable.

    Thanks for the feedback. :)

    Tom
     
  10. Sync1B

    Sync1B

    Joined:
    Sep 13, 2005
    Posts:
    561
    Yeah I have to say I love previewing my game quickly but my dock drives me nuts!!! (witch is a firsty) It just gets in the way. I think there should be a way for the mouse to dissapear while testing.

    Bill
     
  11. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I haven't tried using mouse input with Unity yet, but in previous non-Unity projects, mouse input spilling out of the game window has been a real pain.

    I'm told that the CGAssociateMouseAndMouseCursorPosition() function provides a means of locking down the mouse so that it won't move, but can still emit deltas. It might be good to do this while playing the game in the editor, or at least to have this as a per-project preference.

    Even outside of the editor, interesting things happen if you have multiple monitors and the mouse position is read directly. It's possible to move the mouse right out of the monitor which contains the game display, and that might generate out-of-range mouse coordinates if the game's scripts don't carefully clamp the coordinates to fit the screen.

    I'm inclined to say that a delta-based custom mouse pointer is the best idea, preferably with the aid of CGAssociateMouseAndMouseCursorPosition(false) set by Unity on starting the game.
     
  12. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Despite being wary of poorly documented Quartz calls (these things usually bite back in the future), I decided to go ahead and give this a try. (CGAssociateMouseAndMouseCursorPosition())

    I made the a bundle for a plugin, dropped it into an Assets/Plugin folder, and ... Unity won't recognize the DllImport directive. I'm getting an error reporting that the type or namespace name 'DllImport' could not be found. I've tried putting the directive in various places in a C# script, but no luck. I've taken a look at the MIDI example (though I have not tried to run it) and the Plugins.html document in the docs, but still can't figure out what's wrong.

    Has anyone gotten the DllImport() directive to work?

    Tom
     
  13. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Did you include:

    using System.Runtime.InteropServices;

    ?
    -Jon
     
  14. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Jon -- The InteropServices was the missing piece - thanks!

    Neil -- The CGAssociateMouseAndMouseCursorPosition() function works. The mouse cursor remains, but it no longer responds to mouse input. The state seems to be tied to the Unity process, so when Unity quits, the mouse control returns to normal. I haven't tried re-enabling the mouse behavior, yet, but I assume that shouldn't be a problem. Just need to find a convenient "cleanup" method that gets called when Unity terminates the game.

    Thanks!

    Since the function in question seems to be relatively undocumented by Apple, I dug up the header file that declares it. Inside there are some interesting comments about its use, and a tantalizing reference to CGWarpMouseCursorPosition(). Here are the comments from CGRemoteOperation.h:

    Code (csharp):
    1.  
    2. /*
    3.  * Helper function to connect or disconnect the mouse and mouse cursor while the calling app
    4.  * is in the foreground.
    5.  *
    6.  * While disconnected, mouse move and drag events will reflect the current position of
    7.  * the mouse cursor position, which will not change with mouse movement. Use the
    8.  * <CoreGraphics/CGDirectDisplay.h> function:
    9.  *
    10.  *  void CGGetLastMouseDelta( CGMouseDelta * deltaX, CGMouseDelta * deltaY );
    11.  *
    12.  * This will report mouse movement associated with the last mouse move or drag event.
    13.  *
    14.  * To update the display cursor position, use the function defined in this module:
    15.  *
    16.  *  CGError CGWarpMouseCursorPosition( CGPoint newCursorPosition );
    17.  *
    18.  * Note: The Force Quit key combination (CMD-OPT-ESC by default) will reconnect the mouse and cursor.
    19.  */
    20. CG_EXTERN CGError CGAssociateMouseAndMouseCursorPosition(boolean_t connected);
    21.  
    Of course, this stuff is certainly not cross-platform, and possibly not even portable across Mac OS X versions. It does open up interesting possibilities for the Unity IDE, though. Caveat utilitor.

    Tom
     
  15. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
  16. tsphillips

    tsphillips

    Joined:
    Jan 9, 2006
    Posts:
    359
    Works great, Joachim - thanks.

    I now attach the below code to the main camera, and any project that uses it will disable the system mouse while playing the game.

    MouseDisable.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Runtime.InteropServices;
    5.  
    6. public class MouseDisable : MonoBehaviour {
    7.  
    8.     [DllImport ("mouseoff")]
    9.     private static extern int SystemMouseEnabled(int state);
    10.  
    11.     // Use this for initialization
    12.     void Awake () {
    13.         SystemMouseEnabled(0);
    14.     } // Awake()
    15.    
    16.     void OnApplicationQuit() {
    17.         SystemMouseEnabled(1);
    18.     } // OnApplicationQuit()
    19.  
    20. }
    21.  
    main.c (compiled to mouseoff.bundle and dropped into Assets/Plugins)
    Code (csharp):
    1.  
    2. extern int SystemMouseEnabled(int state);
    3.  
    4. int SystemMouseEnabled(int state) {
    5.  
    6.     if (state == 0) {
    7.         CGAssociateMouseAndMouseCursorPosition (false);
    8.     } else {
    9.         CGAssociateMouseAndMouseCursorPosition (true);
    10.     } /* if-else */
    11.     return state;
    12. } /* SystemMouseEnabled() */
    13.  
    Anyways, using this code makes testing in the editor much more user-friendly, as now I'm not constantly clobbering the dock, etc.