Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Input/refresh issues in exported game.

Discussion in 'Editor & General Support' started by Tristad, Jun 20, 2020.

  1. Tristad

    Tristad

    Joined:
    Jun 10, 2020
    Posts:
    7
    First, i'm so sorry for my bad english, i'm italian.
    I'm new in the unity world, i'm pretty sure that my issue have a very easy solution, so sorry.

    I'm trying to make a little 1st person 3D game in unity. I made camera rotation controls, 3rd person and movements. Everything looks like perfect. I usually export my game after any big change and i try the build. Every build look like working fine as the game, in the editor but yesterday, after i exported my game the sensivity of the mouse became very low, if i press any wasd key for a very small amount of time, the player doesn't move and it move only if i hold the key down for at least 1-2 seconds. When i press Z (the key to switch to the 3rd person) the camera movement should be smooth (i used Vector3.Lerp()) as in the editor, but in the build the camera just teleport in the 3rd person position and more other issues.

    If i try to run the game in the editor, it works perfectly, but the exported version have many problems.
    I also figured out that EVERY other previous version that i exported, now have the same issues.
    I sent the exported game to my friend and in his pc, everything run perfectly.

    I started thinking my pc is the problem.

    The video in which i show the problem:


    i really don't know what to do... can someone help me please????
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Your movement scripts are probably not taking into account Time.deltaTime, and are therefore framerate-dependent.

    Hard to tell for sure without seeing your code.
     
  3. Tristad

    Tristad

    Joined:
    Jun 10, 2020
    Posts:
    7
    PlayerMovements.cs:
    Code (CSharp):
    1. if (camMode) { // 3rd person
    2.             cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, new Vector3(cam.transform.localPosition.x, cam.transform.localPosition.y, defaultCamZ - 10f), 0.018f);
    3.         } else {
    4.             cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, new Vector3(cam.transform.localPosition.x, cam.transform.localPosition.y, defaultCamZ), 0.11f);
    5.         }
    6.  
    7.         float horizInput = Input.GetAxis(horizontalInputName);
    8.         float vertInput = Input.GetAxis(verticalInputName);
    9.  
    10.         Vector3 forwardMovement = transform.forward * vertInput;
    11.         Vector3 rightMovement = transform.right * horizInput;
    12.  
    13.         if (!camMode) charController.SimpleMove(Vector3.ClampMagnitude(forwardMovement + rightMovement, 1.0f) * movementSpeed); else charController.SimpleMove(Vector3.zero);
    14.  
    15.         if ((horizInput != 0 || vertInput != 0) && OnSlope()) {
    16.             charController.Move(Vector3.down * charController.height / 2 * slopeForce * Time.deltaTime);
    17.         }
    18.  
    19.         SetMovementSpeed();
    20.         JumpInput();
    21.  
    22.     }
    23.  
    24.     private void SetMovementSpeed() {
    25.         if(Input.GetKey(runKey)) {
    26.             movementSpeed = Mathf.Lerp(movementSpeed, runSpeed, Time.deltaTime * runBuildUpSpeed);
    27.         } else {
    28.             movementSpeed = Mathf.Lerp(movementSpeed, walkSpeed, Time.deltaTime * runBuildUpSpeed);
    29.         }
    30.     }
    PlayerLook.cs:
    Code (CSharp):
    1. private void CameraRotation()
    2.     {
    3.         float mouseX = Input.GetAxis(mouseXInputName) * sensivityX * Time.deltaTime;
    4.         float mouseY = Input.GetAxis(mouseYInputName) * sensivityY * Time.deltaTime;
    5.  
    6.         xAxisClamp += mouseY;
    7.  
    8.         if(xAxisClamp > 90.0f)
    9.         {
    10.             xAxisClamp = 90.0f;
    11.             mouseY = 0.0f;
    12.             ClampXAxisRotationToValue(270.0f);
    13.         }
    14.         else if (xAxisClamp < -90.0f)
    15.         {
    16.             xAxisClamp = -90.0f;
    17.             mouseY = 0.0f;
    18.             ClampXAxisRotationToValue(90.0f);
    19.         }
    20.  
    21.         //playerBody.Rotate(new Vector3(-1 * mouseY, 1 * mouseX, 0));
    22.         //playerBody.rotation = Quaternion.Euler(playerBody.rotation.eulerAngles.x, playerBody.rotation.eulerAngles.y, 0f);
    23.  
    24.         transform.Rotate(Vector3.left * mouseY);
    25.         playerBody.Rotate(Vector3.up * mouseX);
    26.     }
    27.  
    28.     private void ClampXAxisRotationToValue(float value)
    29.     {
    30.         Vector3 eulerRotation = transform.eulerAngles;
    31.         eulerRotation.x = value;
    32.         transform.eulerAngles = eulerRotation;
    33.     }
     
  4. Tristad

    Tristad

    Joined:
    Jun 10, 2020
    Posts:
    7
    Ok now i got the Time.deltaTime value...

    When i run the game in the editor the deltaTime value is between around 0.003xxx and 0.008xxx.
    When i run the exported game, the deltaTime value is between around 0.0005xxx and 0.0009xxx.

    Isn't it strange? In the exported game, according to your idea, the deltaTime should be more than the deltaTime in the editor game.

    I'm so confused :(
     
  5. Tristad

    Tristad

    Joined:
    Jun 10, 2020
    Posts:
    7
    Can someone help me please??
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Nope, the framerate is higher in the exported game because there isn't any editor overhead. Higher framerate means lower deltaTime.
     
  7. Tristad

    Tristad

    Joined:
    Jun 10, 2020
    Posts:
    7
    So is there any solution?
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You need to design your game to be frame rate independent. As it is you have designed it so it will only work correctly at the specific frame rate your specific computer produces while in the editor. That won't do obviously.

    Typically you do so by multiplying anything which should occur over time with Time.deltaTime, since as you noticed Time.deltaTime changes with the current frame rate.

    Looking at your Mathf.Lerp calls in SetMovementSpeed, it looks like you're doing it wrong. The 3rd float parameter you pass to it needs to be a value between 0f and 1f. Do the math on what you are putting in there. It is supposed to be "what percentage between the two other floats do I want to return?" If you want that to be smoothed over time you instead do something like this:

    Code (csharp):
    1. float percent = 0f;
    2. void Update()
    3. {
    4.     percent = percent + (0.01f * Time.deltaTime);
    5.     if (percent > 1f)
    6.     {
    7.         percent = 1f;
    8.     }
    9. }
    The above code will create a smooth transition between 0f and 1f entirely frame rate independent in the variable "percent", and take about 100 seconds to complete. That is the kind of thing you would feed into a Lerp call's 3rd parameter to create a smooth frame rate independent transition between the two other values you give it.

    edit:
    On top of that, each frame you're using your previous frame's movementSpeed as the first parameter in the Lerp. That is rarely what you want to do. You want to save the starting speed and the target end speed and Lerp between them. Don't use the returned value from the last time you called Lerp as any of the parameters you feed into the next Lerp. This is a common mistake. This is another thing you can see clearly if you just do the math on it to see why. Say you start at 5f and want to get to 10f, so you call Lerp and pass in 0.2f as the 3rd parameter. That will result in a returned value of about 6f. If on the next call you want to move another 20% closer to 10f, you then pass in 0.4f as the 3rd parameter. That will get you a returned value of 7f as expected only so long as you pass in 5f and 10f as the first two parameters again. But, if you pass in the current value of 6f (like you're doing with movementSpeed) and 10f, the Lerp will instead return about 7.6f, which is more than 50% of the way to 10f, not 40%. The speed of whatever you are Lerping will feel off, not at all smooth.

    Your other Lerp calls don't make much sense to me either, but I am not sure what you are trying to do with them.
     
    Last edited: Jun 24, 2020
    PraetorBlue likes this.
  9. Tristad

    Tristad

    Joined:
    Jun 10, 2020
    Posts:
    7
    Really thanks man, i'm gonna try it. But what for the really slow sensivity when i look around?

    edit: the other lerp are for a smooth camera movement as when you press Z like in the video (but it works only in the editor, not in my pc :().

    second edit: Thanks to you, now my camera movement is perfectly smooth but i can't fix the wasd issue. In the exported game f i press it for a very little amount of time, the player doen't move, but in the editor it does. I also can't solve the sensitivity problem :(.
     
    Last edited: Jun 24, 2020
  10. Tristad

    Tristad

    Joined:
    Jun 10, 2020
    Posts:
    7
    After about 2 weeks i still can't solve the problem... please can someone help me?