This repository was archived by the owner on Mar 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSyncPosition.cs
More file actions
91 lines (85 loc) · 3.24 KB
/
SyncPosition.cs
File metadata and controls
91 lines (85 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using MLAPI.MonoBehaviours.Core;
using System.IO;
using UnityEngine;
public class SyncPosition : NetworkedBehaviour
{
private float lastSentTime;
public float PosUpdatesPerSecond = 20;
private void Awake()
{
if(isServer)
{
// RegisterMessageHandler("PositionUpdate", OnRecievePositionUpdate);
}
if(isClient)
{
// RegisterMessageHandler("SetClientPosition", OnSetClientPosition);
}
}
private void Update()
{
if (!isLocalPlayer)
return;
if(Time.time - lastSentTime > (1f / PosUpdatesPerSecond))
{
using(MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(transform.position.x);
writer.Write(transform.position.y);
writer.Write(transform.position.z);
}
SendToServer("PositionUpdate", "PositionUpdates", stream.ToArray());
}
lastSentTime = Time.time;
}
transform.Translate(new Vector3(Input.GetAxis("Horizontal") * Time.deltaTime, Input.GetAxis("Vertical") * Time.deltaTime, 0));
}
//This gets called on all clients except the one the position update is about.
void OnSetClientPosition(uint clientId, byte[] data)
{
using (MemoryStream stream = new MemoryStream(data))
{
using (BinaryReader reader = new BinaryReader(stream))
{
uint targetNetId = reader.ReadUInt32();
if (targetNetId != networkId)
return;
float x = reader.ReadSingle();
float y = reader.ReadSingle();
float z = reader.ReadSingle();
GetNetworkedObject(targetNetId).transform.position = new Vector3(x, y, z);
}
}
}
//This gets called on the server when a client sends it's position.
void OnRecievePositionUpdate(uint clientId, byte[] data)
{
//This makes it behave like a HLAPI Command. It's only invoked on the same object that called it.
if (clientId != ownerClientId)
return;
using (MemoryStream readStream = new MemoryStream(data))
{
using (BinaryReader reader = new BinaryReader(readStream))
{
float x = reader.ReadSingle();
float y = reader.ReadSingle();
float z = reader.ReadSingle();
transform.position = new Vector3(x, y, z);
}
using (MemoryStream writeStream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(writeStream))
{
writer.Write(networkId);
writer.Write(transform.position.x);
writer.Write(transform.position.y);
writer.Write(transform.position.z);
}
//Sends the position to all clients except the one who requested it. Similar to a Rpc with a if(isLocalPlayer) return;
SendToNonLocalClients("SetClientPosition", "PositionUpdates", writeStream.ToArray());
}
}
}
}