Skip to content

Commit d91b16c

Browse files
committed
docs: Added documented example NetworkedBehaviour
1 parent 337a173 commit d91b16c

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using MLAPI;
3+
using MLAPI.Messaging;
4+
using UnityEngine;
5+
6+
namespace MLAPI_Examples
7+
{
8+
public class BasicRpcPositionSync : NetworkedBehaviour
9+
{
10+
// Send position no more than 20 times a second
11+
public int SendsPerSecond = 20;
12+
// Dont send position unless we have moved at least 1 cm
13+
public float MinimumMovement = 0.01f;
14+
// Dont send position unless we have rotated at least 1 degree
15+
public float MinimumRotation = 1f;
16+
// Allows you to change the channel position updates are sent on. Its prefered to be UnreliableSequenced for fast paced.
17+
public string Channel = "MLAPI_DEFAULT_MESSAGE";
18+
19+
private float lastSendTime;
20+
private Vector3 lastSendPosition;
21+
private Quaternion lastSendRotation;
22+
23+
public override void NetworkStart()
24+
{
25+
// This is called when the object is spawned. Once this gets invoked. The object is ready for RPC and var changes.
26+
27+
// Set the defaults to prevent a position update straight after spawn with the same redundant values. (The MLAPI syncs positions on spawn)
28+
lastSendPosition = transform.position;
29+
lastSendRotation = transform.rotation;
30+
}
31+
32+
private void Update()
33+
{
34+
// Check if its time to send a new position update
35+
if (IsOwner && Time.time - lastSendTime > (1f / SendsPerSecond))
36+
{
37+
// Check if we have moved enough or rotated enough for a position update
38+
if (Vector3.Distance(lastSendPosition, transform.position) >= MinimumMovement || Quaternion.Angle(lastSendRotation, transform.rotation) > MinimumRotation)
39+
{
40+
// We moved enough.
41+
42+
// Set the last states
43+
lastSendTime = Time.time;
44+
lastSendPosition = transform.position;
45+
lastSendRotation = transform.rotation;
46+
47+
if (IsClient)
48+
{
49+
// If we are a client. (A client can be either a normal client or a HOST), we want to send a ServerRPC. ServerRPCs does work for host to make code consistent.
50+
InvokeServerRpc(SendPositionToServer, transform.position, transform.rotation, Channel);
51+
}
52+
else if (IsServer)
53+
{
54+
// This is a strict server with no client attached. We can thus send the ClientRPC straight away without the server inbetween.
55+
InvokeClientRpcOnEveryone(SetPosition, transform.position, transform.rotation, Channel);
56+
}
57+
}
58+
}
59+
}
60+
61+
[ServerRPC(RequireOwnership = true)]
62+
public void SendPositionToServer(Vector3 position, Quaternion rotation)
63+
{
64+
// This code gets ran on the server at the request of clients or the host
65+
66+
// Tell every client EXCEPT the owner (since they are the ones that actually send the position) to apply the new position
67+
InvokeClientRpcOnEveryoneExcept(SetPosition, OwnerClientId, position, rotation, Channel);
68+
}
69+
70+
[ClientRPC]
71+
public void SetPosition(Vector3 position, Quaternion rotation)
72+
{
73+
// This code gets ran on the clients at the request of the server.
74+
75+
transform.position = position;
76+
transform.rotation = rotation;
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)