Remote Events
5 - 10 minutes read
It's time for some shooting! You might have noticed that the shooting action is not synced. In this section, you will add a remote event to the Player Prefab to sync player actions.

Remote Event Agent

Select the Player Prefab. Click the Add Component button and Search "Agent", select Remote Event Agent to attach it to the Player GameObject.
Click on the Add Remote Event button and set the Event Name to "fire".
The Event name is used to find and trigger event across different network. One Remote Event Agent cannot have remote events that have the same name.
The SWNetwork Remote Events are similar to the Unity events. You can invoke them in scripts. You can add listeners to them to handle the events. However, SWNetwork Remote Events when invoked will be triggered on all the remote duplicates.
You need to update the PlayerWeapon.cs script to invoke and handle the "fire" event.
Add two properties: networkId, and remoteEventAgent.
1
NetworkID networkId;
2
RemoteEventAgent remoteEventAgent;
Copied!
Implement the Start() method and initialize the networkId, and remoteEventAgent properties.
1
void Start()
2
{
3
networkId = GetComponent<NetworkID>();
4
remoteEventAgent = gameObject.GetComponent<RemoteEventAgent>();
5
}
Copied!
Only the source Player GameObject should receive user inputs, we can use the IsMine property of NetworkID to find out if the Player GameObject is the source GameObject.
1
void Update()
2
{
3
if (networkId.IsMine)
4
{
5
// get mouse inputs
6
if (Input.GetMouseButtonDown(0))
7
{
8
// 0.5 seconds interval between shots
9
if (!IsInvoking("FireBullet"))
10
{
11
InvokeRepeating("FireBullet", 0f, 0.5f);
12
}
13
}
14
15
if (Input.GetMouseButtonUp(0))
16
{
17
CancelInvoke("FireBullet");
18
}
19
}
20
}
Copied!
Update the fireBullet() method to invoke the "fire" event. We used SWNetworkMessage to send the bullet launch position(vector3) and aim direction (vector3) along with the "fire" event.
1
void FireBullet()
2
{
3
SWNetworkMessage msg = new SWNetworkMessage();
4
msg.Push(launchPosition.position);
5
msg.Push(transform.forward);
6
remoteEventAgent.Invoke("fire", msg);
7
}
Copied!
Implement the Event Handler method. We used PopVector3() method on the SWNetworkMessage object to obtain the launch position and aim direction of the fire remote event.
1
public void RemoteFire(SWNetworkMessage msg)
2
{
3
Vector3 position = msg.PopVector3();
4
Vector3 direction = msg.PopVector3();
5
gun.fireBullet(position, direction);
6
}
Copied!
The final version of PlayerWeapon.cs script should look like.
1
using UnityEngine;
2
using SWNetwork;
3
4
public class PlayerWeapon : MonoBehaviour
5
{
6
// aimable layers
7
public LayerMask layerMask;
8
9
private Vector3 currentLookTarget = Vector3.zero;
10
public Gun gun;
11
12
// launch position of bullets
13
public Transform launchPosition;
14
15
NetworkID networkId;
16
RemoteEventAgent remoteEventAgent;
17
18
private void Start()
19
{
20
networkId = GetComponent<NetworkID>();
21
remoteEventAgent = gameObject.GetComponent<RemoteEventAgent>();
22
}
23
24
void FixedUpdate()
25
{
26
if (networkId.IsMine)
27
{
28
// find player's cursor position in the environment
29
RaycastHit hit;
30
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
31
Debug.DrawRay(ray.origin, ray.direction * 1000, Color.green);
32
if (Physics.Raycast(ray, out hit, 1000, layerMask, QueryTriggerInteraction.Ignore))
33
{
34
if (hit.point != currentLookTarget)
35
{
36
currentLookTarget = hit.point;
37
}
38
}
39
40
// ignore cursor position's y value.
41
Vector3 targetPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
42
43
// calculate player's new rotation
44
Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
45
46
// lerp
47
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 10.0f);
48
}
49
}
50
51
void Update()
52
{
53
if (networkId.IsMine)
54
{
55
// get mouse inputs
56
if (Input.GetMouseButtonDown(0))
57
{
58
// 0.5 seconds interval between shots
59
if (!IsInvoking("FireBullet"))
60
{
61
InvokeRepeating("FireBullet", 0f, 0.5f);
62
}
63
}
64
65
if (Input.GetMouseButtonUp(0))
66
{
67
CancelInvoke("FireBullet");
68
}
69
}
70
}
71
72
void FireBullet()
73
{
74
SWNetworkMessage msg = new SWNetworkMessage();
75
msg.Push(launchPosition.position);
76
msg.Push(transform.forward);
77
remoteEventAgent.Invoke("fire", msg);
78
}
79
80
public void RemoteFire(SWNetworkMessage msg)
81
{
82
Vector3 position = msg.PopVector3();
83
Vector3 direction = msg.PopVector3();
84
gun.FireBullet(position, direction);
85
}
86
}
Copied!
Add a listener to the "fire" event. Make sure to select the RemoteFire() method under the Dynamic Parameter Section of the methods list.
Make sure you applied the change to the Player Prefab.
Double check if the player prefab has been saved correctly by inspecting the Player prefab in the /Assets/prefabs folder. Make sure the RemoteEventAgent is saved and the prefab is enabled.

Play

Make sure you disabled the Player GameObject in the Hierarchy.
You can follow the steps in the last section "Test and Play" to test out the "fire" remote event.
Copy link