Search Unity

Build is 3 - 4x faster than the host

Discussion in 'Scripting' started by TheOtherUserName, Oct 10, 2021.

  1. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I didn't know if I should post it in the Multiplayer Discussion section or scripting but since I am pretty sure I'm doing something wrong code wise I posted it here.
    So I use the MLAPI from Unity to make a multiplayer game and it works well except for the problem that the build is 3 - 4x times faster than the unity editor. I made my player movement server controlled to avoid people from teleporting.
    This is the code:
    Code (CSharp):
    1. void Update()
    2. {
    3.     //The keys[] array is a bollean array to get the events of the movement keys being pressed
    4.     keys[0] = Input.GetKey(KeyCode.W);
    5.     keys[1] = Input.GetKey(KeyCode.A);
    6.     keys[2] = Input.GetKey(KeyCode.S);
    7.     keys[3] = Input.GetKey(KeyCode.D);
    8.  
    9.     //function to calculate the new player position
    10.     SubmitNewPosition(keys);
    11.     //Set the player position to the networked variable playerPos
    12.     transform.position = playerPos.Value;
    13. }
    14.  
    15. void SubmitNewPosition(bool[] keys)
    16. {
    17.     Vector3 newPos = playerPos.Value;
    18.  
    19.     if(keys[0])
    20.     {
    21.         //I know it is ugly and I shouldn't do it but for testing purposes I modified the position like this
    22.         newPos.z += speed/* speed is a public float equal to 5f*/ * (1f / 60f) /*Fixed tick*/;
    23.     }
    24.  
    25.     if(keys[1])
    26.     {
    27.         newPos.x -= speed * (1f / 60f);
    28.     }
    29.  
    30.     if(keys[2])
    31.     {
    32.         newPos.z -= speed * (1f / 60f);
    33.     }
    34.  
    35.     if(keys[3])
    36.     {
    37.         newPos.x += speed * (1f / 60f);
    38.     }
    39.     playerPos.Value = newPos;
    40. }
    On the unity side everything works fine. Meaning this problem only happens in a build. I don't understand why but runing the build the player moves way faster.
    I am using Unity 2021.1.3f1
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You manually multiply with 1/60, but are you making sure the thing runs at a constant and unchanging 60 FPS?
    Usually we multiply with TIme.deltaTime to get that same value, but depending on current frame rate.
     
  3. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    I do that because in the future the server/host will have a tick of 1/60 to ensure that the player only moves in ranges that are reachable in this time frame. Else the client could give a time of idk 50 and move at insane speeds. At least thats my thought process behind that.
     
  4. matzomat

    matzomat

    Joined:
    Mar 4, 2018
    Posts:
    63
    You must use Time.Deltatime or you‘ll be Framerate dependant because you don’t know how many times per second update() is called.
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Still use Time.deltaTime, and enforce the tick rate some other way. That way if load on your server changes and it cant handle 60 ticks a second anymore, your code wouldnt break. As you did it right now, server load may affect game speed. And that appears to be exactly what you are experiencing.
    As for why it happens in build and not in editor, there are certain (debugging, editor tools, ..) things running which are turned off in build mode, so the actual build usually has a slightly better performance compared to the editor version. However, this wont be a difference of 3-4x.
     
    matzomat likes this.
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    Also, having V-sync enabled might be masking the problem on editor.
     
    matzomat and Yoreki like this.