Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved FPS Dependant movement

Discussion in 'Scripting' started by DPunK, Apr 4, 2021.

  1. DPunK

    DPunK

    Joined:
    May 15, 2019
    Posts:
    25
    Every since I upgraded my project from 2018.4.10f1 to 2021.1.0f1 I've noticed a huge increase in my FPS inside the editor. It goes up to 300 when I look at an empty scene but drops dramatically to 30~ when I look at a lot of objects.

    Now this by itself is not a problem as my scene is not optimized in the least.
    However, it causes my movement and update based functions to slow down or speed up.
    I am using Time.deltaTime to scale every change and this seems to cause to opposite reaction from my frame rate.

    For example, camera rotation speeds up when fps is low and slows down to a crawl when it's high.
    This make sense because higher frame rate means less delta between frames but I never had this problem. As far as I know multiplying by the deltaTIme should remove all inconsistencies and make it run smooth no matter the FPS.

    I also player with the vSync options in quality to try and reduce or clamp the FPS but nothing affected it
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    We can't tell you what's wrong with your code if we can't see your code.
     
  4. DPunK

    DPunK

    Joined:
    May 15, 2019
    Posts:
    25
    This is the code snippet in charge of camera movement
    Would appreciate if someone can point to what might be the cause
    Code (CSharp):
    1. void LateUpdate()
    2.  
    3. void LateUpdate()
    4.     {
    5.         float mouseX = Input.GetAxis("Mouse X") * sensitivity.x * Time.deltaTime;
    6.         float mouseY = Input.GetAxis("Mouse Y") * sensitivity.y * Time.deltaTime;
    7.  
    8.         xRotation -= mouseY;
    9.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    10.  
    11.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    12.        
    13.         switch(_lookState)
    14.         {
    15.             case LookState.TURN_BODY:
    16.             playerBody.Rotate(Vector3.up * mouseX);
    17.             break;
    18.  
    19.             case LookState.TURN_HEAD:
    20.             break;
    21.         }
    22.     }
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Multiplying mouse axis input by Time.deltaTime is unecessary and will introduce framerate-dependent jitter into your game. Mouse axis data is already framerate independent.
    https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
     
    angrypenguin, exiguous and StarManta like this.
  6. DPunK

    DPunK

    Joined:
    May 15, 2019
    Posts:
    25
    Thanks PraetorBlue, that was it!
    I never knew that this function was not frame dependant..
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Just be careful. Most axes like the built in "Horizontal" and "Vertical" axes behave in a way that they will be framerate dependent if you blindly apply their result to some kind of motion in game.

    It's not the function itself but that particular axis ("Mouse X" and "Mouse Y" which is framerate independent.
     
    Last edited: Jan 9, 2023
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    The way I think about this aspect of user inputs like this is: If I as a player should be able to achieve something by "holding" the input at a particular state, then the input must be multiplied by Time.deltaTime to achieve correct behavior with different framerates.

    ex: Holding "up" key => Character continually runs forward => needs deltaTime
    Holding the joystick in the "right" position => camera continually rotates to the right => needs deltaTime
    But holding the mouse to the right of where it was => the camera stops rotating as soon as you stop moving the mouse => NO deltaTime
     
  9. GreenTDM

    GreenTDM

    Joined:
    Apr 12, 2021
    Posts:
    1
    THANK YOU SO MUCH THIS THREAD IS GONNA HELP ME SO MUCH
     
    blueivy likes this.