Search Unity

Mouse Cursor questions.

Discussion in 'Editor & General Support' started by Juan, Jul 2, 2009.

  1. Juan

    Juan

    Joined:
    May 21, 2009
    Posts:
    142
    I have a few questions.

    I have a game wher you runs a boat, you control the boat with the X axis of the mouse, you turn left in negative x and turn right in positive x.

    The problem is: i need to continue turning but i need that the cursor stays inside the game window, and if it's possible i need to relocate the cursor at the x=0 when the cursor arrives the screen.width, in my case 800px.

    So i need the mouse position to be 0 in x when it gets to 800 in x.

    Help please, that's important because i cannot modify this, is the way a hardware that i need to use works, and that hardware controls the mouse.

    Cheers and thanks :D
     
  2. cyb3rmaniak

    cyb3rmaniak

    Joined:
    Dec 10, 2007
    Posts:
    162
    So... you have two issues here as I can see...
    The first is that right now you're not exactly using the Axis of the mouse in the best way, but more like you're using the position of the mouse.
    Axis information (using Input.GetAxis()) returns a delta of the mouse each frame. You handle how those deltas are added or subtracted, so you can easily reset the position from 800 to 0...
    The second issue is that you want to move the mouse cursor... Not possible right now.
    BUT, you CAN hide the mouse cursor, and display your own if you like to - using GUI or any way you want to. Then you can do whatever you want with it.

    Now, Lets assume you save your current mouse position in two floats - fPositionX and fPositionY.
    each frame you add to fPositionX the X delta the mouse moved in the last frame horizontally...
    Code (csharp):
    1.  
    2. fPositionX += Input.GetAxis("MyMouseDeltaX") * fMouseSmoothing;
    3. if (fPositionX > Screen.width)
    4. {
    5.    fPositionX -= Screen.width;
    6. }
    7. else if (fPositionX < 0)
    8. {
    9.    fPositionX += Screen.width;
    10. }
    11.  
    and if you want to clamp it so it won't leave the screen area you can do something like

    Code (csharp):
    1.  
    2. fPositionX = Mathf.Clamp(fPositionX + Input.GetAxis("MyMouseDeltaX") * fMouseSmoothing, 0, Screen.width);
    3.  
    If you don't need the mouse cursor for other things, I would not use it's position anyway, and just hide it completely and use the Input.GetAxis() to change the rotation of the ship directly.
     
  3. bloodtiger10

    bloodtiger10

    Joined:
    Nov 9, 2008
    Posts:
    619
    uhhhh.. the easiest way is to use the lock cursor script from the fps tutorial. it will hide you cursor and do some other stuff which IS much simpler than what the person above me described.

    it just locks the cursor and that means that it will always stay in your screen. IT doesn't use .Clamp and stuff like that.
     
  4. Juan

    Juan

    Joined:
    May 21, 2009
    Posts:
    142
    The lock cursor solution works well, also is fastest, but i must take a look into cyb3rmaniak solution because maybe is difficult but more practical.

    Thanks
     
  5. bloodtiger10

    bloodtiger10

    Joined:
    Nov 9, 2008
    Posts:
    619
    it is pretty easy to mod as well :)
     
  6. cyb3rmaniak

    cyb3rmaniak

    Joined:
    Dec 10, 2007
    Posts:
    162
    Other stuff? What if I don't want other stuff? What kind of stuff??? I love the specificity in that one. ;)

    Anyways, the point is- after you hide the cursor, you can do whatever you want... I just gave 2 examples.