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:
void Update()
{
//The keys[] array is a bollean array to get the events of the movement keys being pressed
keys[0] = Input.GetKey(KeyCode.W);
keys[1] = Input.GetKey(KeyCode.A);
keys[2] = Input.GetKey(KeyCode.S);
keys[3] = Input.GetKey(KeyCode.D);
//function to calculate the new player position
SubmitNewPosition(keys);
//Set the player position to the networked variable playerPos
transform.position = playerPos.Value;
}
void SubmitNewPosition(bool[] keys)
{
Vector3 newPos = playerPos.Value;
if(keys[0])
{
//I know it is ugly and I shouldn't do it but for testing purposes I modified the position like this
newPos.z += speed/* speed is a public float equal to 5f*/ * (1f / 60f) /*Fixed tick*/;
}
if(keys[1])
{
newPos.x -= speed * (1f / 60f);
}
if(keys[2])
{
newPos.z -= speed * (1f / 60f);
}
if(keys[3])
{
newPos.x += speed * (1f / 60f);
}
playerPos.Value = newPos;
}
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