Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

mouse input issues, framerate dependence weirdness

Discussion in 'Editor & General Support' started by XRA, Oct 27, 2011.

  1. XRA

    XRA

    Joined:
    Aug 26, 2010
    Posts:
    265
    Can't seem to get my mouse control to be framerate independent, I've been trying to figure this out for a while now. Depending on people's computer specs they get different movement speeds in-game.

    I've updated my code to where it might be working, but it still seems like I get a different "feel" when playing the game on a built EXE versus playing in-editor. I notice that the framerate via Fraps is 60 in editor, and then 400 to 500 fps in a built EXE. (even fraps recording video has an effect on the overall behavior of the controls)

    This function is called from Update() and is the only thing affecting my movement.
    I've noticed if I change it to FixedUpdate() everything will move slower since it is locking to roughly the default 50fps, if the framerate dips below 50 everything will move faster since the mouse is moving "further" each frame (since the time between frames is larger).
    With it as Update(), the reverse happens... the higher the framerate the seemingly faster/more sensitive the mouse movement is, while the lower the framerate, the less sensitive it is.


    It is wrong to multiply the mouse input by deltaTime, right? What about when using GetAxisRaw ??

    I'm using Lerp and Slerp with a couple tweaked delta*N values to get the right kind of deceleration/acceleration smoothing behavior... is there a different way I should be doing that stuff?

    *edit* also if I change to GetAxisRaw it works without interruption in the editor when fraps drops the framerate to 30 when recording video, the controls stay perfect. But when I run the built game the mouse cursor hardly moves since the framerate is high.. if I record video (so that it drops framerate) the mouse cursor moves normal.

    Any ideas why there are there these inconsistencies with the mouse input and framerate? What is wrong with my code, or is there a bug with Unity itself?

    Code (csharp):
    1.  
    2. void SpinCrap () {
    3.     float delta = Time.deltaTime;
    4.     if(coreGuide != null){         
    5.         float axisY = Input.GetAxis("Mouse Y");
    6.         float axisX = Input.GetAxis("Mouse X");
    7.         Vector3 regularRotation = new Vector3(axisY,-axisX,0f);
    8.            
    9.         if ( regularRotation != lastRegularRotation ){
    10.             lastRegularRotation = regularRotation;
    11.             avgSpeed = Vector3.Lerp(avgSpeed,regularRotation, delta*4f);
    12.             speed = avgSpeed;
    13.         } else {
    14.             speed = Vector3.Lerp( avgSpeed, Vector3.zero, delta*0.5f);
    15.         }
    16.         pointerPivot.Rotate( Camera.main.transform.up * speed.y, Space.World );
    17.             pointerPivot.Rotate( Camera.main.transform.right * speed.x, Space.World );
    18.         cameraPivot.transform.rotation = Quaternion.Slerp(cameraPivot.transform.rotation,pointerPivot.rotation,delta*10f);//10f
    19.         cameraTarget.transform.position = cameraPivot.transform.position;
    20.         Quaternion lastPos = cameraTarget.transform.rotation;
    21.         cameraTarget.transform.rotation = Quaternion.Slerp(lastPos, cameraPivot.transform.rotation, delta*5f); //5
    22.     }
    23. }
    24.  
     
    Last edited: Oct 27, 2011
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Mouse input is already framerate-independent; if you multiply it by Time.deltaTime, then you make it framerate-dependent.

    --Eric
     
    Deleted User and crawler like this.
  3. XRA

    XRA

    Joined:
    Aug 26, 2010
    Posts:
    265
    okay that makes sense, but from the code snippet above, does that mean I am ultimately multiplying it by deltaTime since I'm using it with Lerp to add smoothing and a "gliding" deceleration when the mouse isn't moving? (the bit with the speed and avgSpeed) That's what I'm having trouble wrapping my head around... cause right now the code above gives different results on different systems.

    In general I'm also wondering if this means that even if I solve the framerate independence stuff, will my mouse input still handle different across various systems simply due to mouse drivers? Is that the benefit of using GetAxisRaw, to avoid any system-applied settings of the mouse? (some of these things may already be answered, I should do another browse through Unity Answers)

    Code (csharp):
    1.         Vector3 regularRotation = new Vector3(axisY,-axisX,0f);
    2.            
    3.         if ( regularRotation != lastRegularRotation ){
    4.             lastRegularRotation = regularRotation;
    5.             avgSpeed = Vector3.Lerp(avgSpeed,regularRotation, delta*4f);
    6.             speed = avgSpeed;
    7.         } else {
    8.             speed = Vector3.Lerp( avgSpeed, Vector3.zero, delta*0.5f);
    9.         }
    10.  
     
  4. crawler

    crawler

    Joined:
    Aug 9, 2012
    Posts:
    27
    OMG! Thank you very mush Eric!!
     
  5. Dannark

    Dannark

    Joined:
    Jul 24, 2016
    Posts:
    14
    Oh jee. OK thats why. It works now like it should be, Thanks!
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,161
    Then make your own forum post rather than necro-posting some random ancient thread.