Player stutter fix

This commit is contained in:
brausjonas
2022-05-13 16:41:00 +02:00
parent 07861676d6
commit fb04675716
6 changed files with 69 additions and 36 deletions
@@ -57,7 +57,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6443714940181712877} m_GameObject: {fileID: 6443714940181712877}
m_LocalRotation: {x: -0.7071068, y: 0, z: 0, w: 0.7071068} m_LocalRotation: {x: -0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0, z: 0.024} m_LocalPosition: {x: 0, y: -0.132, z: 0.024}
m_LocalScale: {x: 0.24999988, y: 0.24999988, z: 0.25} m_LocalScale: {x: 0.24999988, y: 0.24999988, z: 0.25}
m_Children: [] m_Children: []
m_Father: {fileID: 4093383343133656141} m_Father: {fileID: 4093383343133656141}
+2 -2
View File
@@ -665,7 +665,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1934992308} m_GameObject: {fileID: 1934992308}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 1.806, z: 0.036} m_LocalPosition: {x: 0, y: 1.7, z: 0.036}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 1789911868} m_Father: {fileID: 1789911868}
@@ -779,7 +779,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7035241573763573916} m_GameObject: {fileID: 7035241573763573916}
m_LocalRotation: {x: -0.7071068, y: 0, z: 0, w: 0.7071068} m_LocalRotation: {x: -0.7071068, y: 0, z: 0, w: 0.7071068}
m_LocalPosition: {x: 0, y: 0, z: 0.024} m_LocalPosition: {x: 0, y: -0.08, z: 0.024}
m_LocalScale: {x: 0.24999988, y: 0.24999988, z: 0.25} m_LocalScale: {x: 0.24999988, y: 0.24999988, z: 0.25}
m_Children: [] m_Children: []
m_Father: {fileID: 1789911868} m_Father: {fileID: 1789911868}
+22 -18
View File
@@ -133,19 +133,20 @@ public class Client
//1.12 == [1] = 1, [2] = 12 //1.12 == [1] = 1, [2] = 12
public void SendPositionUpdate(Vector3 position) public void SendPositionUpdate(Vector3 position)
{ {
string x = position.x.ToString("F"); string x = position.x.ToString("n4");
string y = position.y.ToString("F"); string y = position.y.ToString("n4");
string z = position.z.ToString("F"); string z = position.z.ToString("n4");
byte xV = byte.Parse(x.Split(',')[0]);
byte xN = byte.Parse(x.Split(',')[1]);
byte yV = byte.Parse(y.Split(',')[0]); string sendPosition = x + " " + y + " " + z;
byte yN = byte.Parse(y.Split(',')[1]); byte[] sendPositionASCII = Encoding.ASCII.GetBytes(sendPosition);
byte[] send = new byte[sendPositionASCII.Length + 1];
send[0] = 7;
for (int i = 1; i < send.Length; i++)
{
send[i] = sendPositionASCII[i - 1];
}
byte zV = byte.Parse(z.Split(',')[0]); Send(send);
byte zN = byte.Parse(z.Split(',')[1]);
Send(new byte[] { 7, xV, xN, yV, yN, zV, zN });
} }
private void Receive() private void Receive()
@@ -171,15 +172,18 @@ public class Client
if (data[0] == 7) if (data[0] == 7)
{ {
string xS = data[1] + "," + data[2]; byte[] positionASCII = new byte[data.Length - 2];
string yS = data[3] + "," + data[4]; for (int i = 0; i < data.Length - 2; i++)
string zS = data[5] + "," + data[6]; {
positionASCII[i] = data[i + 2];
}
float x = float.Parse(xS); byte id = data[1];
float y = float.Parse(yS);
float z = float.Parse(zS);
byte id = data[7]; string[] positionString = Encoding.ASCII.GetString(positionASCII).Split(' ');
float x = float.Parse(positionString[0]);
float y = float.Parse(positionString[1]);
float z = float.Parse(positionString[2]);
world.UpdatePlayerPosition(new Vector3(x, y, z), id); world.UpdatePlayerPosition(new Vector3(x, y, z), id);
} }
@@ -232,5 +232,17 @@ public class Player : MonoBehaviour
return false; return false;
} }
} }
public class PlayerUpdateData
{
public PlayerUpdateData(byte id, Vector3 position)
{
this.id = id;
this.position = position;
}
public byte id;
public Vector3 position;
}
} }
@@ -12,6 +12,7 @@ public class World : MonoBehaviour
private string name; private string name;
private Dictionary<byte, GameObject> otherPlayers = new Dictionary<byte, GameObject>(); private Dictionary<byte, GameObject> otherPlayers = new Dictionary<byte, GameObject>();
private Queue<byte> playersToCreate = new Queue<byte>(); private Queue<byte> playersToCreate = new Queue<byte>();
private Queue<Player.PlayerUpdateData> playersToUpdate = new Queue<Player.PlayerUpdateData>();
//chunks //chunks
private Chunk[,] chunks; private Chunk[,] chunks;
@@ -50,15 +51,18 @@ public class World : MonoBehaviour
player.transform.SetParent(gameObject.transform); player.transform.SetParent(gameObject.transform);
otherPlayers.Add(id, player); otherPlayers.Add(id, player);
} }
if (playersToUpdate.Count > 0)
{
Player.PlayerUpdateData data = playersToUpdate.Dequeue();
otherPlayers.TryGetValue(data.id, out GameObject g);
g.transform.position = data.position;
}
} }
public void UpdatePlayerPosition(Vector3 position, byte id) public void UpdatePlayerPosition(Vector3 position, byte id)
{ {
otherPlayers.TryGetValue(id, out GameObject g); playersToUpdate.Enqueue(new Player.PlayerUpdateData(id, position));
if(g != null)
{
g.transform.position = position;
}
} }
public void AddPlayer(byte id) public void AddPlayer(byte id)
+20 -7
View File
@@ -138,23 +138,36 @@ public class Server
if (data[0] == 7) if (data[0] == 7)
{ {
string xS = data[1] + "," + data[2]; byte[] positionASCII = new byte[data.Length - 1];
string yS = data[3] + "," + data[4]; for (int i = 0; i < data.Length - 1; i++)
string zS = data[5] + "," + data[6]; {
positionASCII[i] = data[i + 1];
}
float x = float.Parse(xS); string[] positionString = Encoding.ASCII.GetString(positionASCII).Split(' ');
float y = float.Parse(yS); float x = float.Parse(positionString[0]);
float z = float.Parse(zS); float y = float.Parse(positionString[1]);
float z = float.Parse(positionString[2]);
int id = GetPlayerID(endPoint); int id = GetPlayerID(endPoint);
playerPositions[id] = new Vector3(x, y, z); playerPositions[id] = new Vector3(x, y, z);
string sendPosition = x + " " + y + " " + z;
byte[] sendPositionASCII = Encoding.ASCII.GetBytes(sendPosition);
byte[] send = new byte[sendPositionASCII.Length + 2];
send[0] = 7;
send[1] = (byte)id;
for (int i = 2; i < send.Length; i++)
{
send[i] = sendPositionASCII[i - 2];
}
foreach (IPEndPoint e in players) foreach (IPEndPoint e in players)
{ {
if (id != GetPlayerID(e)) if (id != GetPlayerID(e))
{ {
Send(new byte[]{7, data[1], data[2], data[3], data[4], data[5], data[6], (byte)id}, e); Send(send, e);
} }
} }
} }