Skip to content
Merged
6 changes: 3 additions & 3 deletions Basic/2DSpaceShooter/Assets/Scripts/Bullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public void SetVelocity(Vector2 velocity)
{
var bulletRb = GetComponent<Rigidbody2D>();
bulletRb.velocity = velocity;
SetVelocityClientRpc(velocity);
ClientSetVelocityRpc(velocity);
}
}

[ClientRpc]
void SetVelocityClientRpc(Vector2 velocity)
[Rpc(SendTo.ClientsAndHost)]
void ClientSetVelocityRpc(Vector2 velocity)
{
if (!IsHost)
{
Expand Down
30 changes: 15 additions & 15 deletions Basic/2DSpaceShooter/Assets/Scripts/NetworkManagerHud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,25 @@ void Start()
ShowInGameUI(false);
ShowStatusText(false);

NetworkManager.Singleton.OnClientConnectedCallback += OnOnClientConnectedCallback;
NetworkManager.Singleton.OnClientDisconnectCallback += OnOnClientDisconnectCallback;
NetworkManager.Singleton.OnConnectionEvent += OnConnectionEvent;
}

void OnOnClientConnectedCallback(ulong obj)
void OnConnectionEvent(NetworkManager networkManager, ConnectionEventData connectionEventData)
{
ShowMainMenuUI(false);
ShowInGameUI(true);
}

void OnOnClientDisconnectCallback(ulong clientId)
{
if ((NetworkManager.Singleton.IsServer && clientId != NetworkManager.ServerClientId))
if (connectionEventData.EventType == ConnectionEvent.ClientConnected)
{
return;
ShowMainMenuUI(false);
ShowInGameUI(true);
}
else if (connectionEventData.EventType == ConnectionEvent.ClientDisconnected)
{
if ((NetworkManager.Singleton.IsServer && connectionEventData.ClientId != NetworkManager.ServerClientId))
{
return;
}
ShowMainMenuUI(true);
ShowInGameUI(false);
}
ShowMainMenuUI(true);
ShowInGameUI(false);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -262,7 +263,6 @@ void OnDestroy()
{
m_ShutdownButton.clickable.clickedWithEventInfo -= ShutdownButtonClicked;
}
m_NetworkManager.OnClientConnectedCallback -= OnOnClientConnectedCallback;
m_NetworkManager.OnClientDisconnectCallback -= OnOnClientDisconnectCallback;
m_NetworkManager.OnConnectionEvent -= OnConnectionEvent;
}
}
23 changes: 11 additions & 12 deletions Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ public override void OnNetworkSpawn()
if (IsServer)
{
LatestShipColor.Value = m_ShipGlowDefaultColor;

PlayerName.Value = $"Player {OwnerClientId}";

if (!IsHost)
Expand Down Expand Up @@ -199,7 +198,7 @@ public void TakeDamage(int amount)

void Fire(Vector3 direction)
{
PlayFireSoundClientRpc();
ClientPlayFireSoundRpc();
var damage = 5;
if (QuadDamageTimer.Value > NetworkManager.ServerTime.TimeAsFloat)
{
Expand Down Expand Up @@ -366,7 +365,7 @@ void UpdateClient()

if (m_OldMoveForce != moveForce || m_OldSpin != spin)
{
ThrustServerRpc(moveForce, spin);
ServerThrustRpc(moveForce, spin);
m_OldMoveForce = moveForce;
m_OldSpin = spin;
}
Expand All @@ -388,7 +387,7 @@ void UpdateClient()
// fire
if (Input.GetKeyDown(KeyCode.Space))
{
FireServerRpc();
ServerFireRpc();
}
}

Expand Down Expand Up @@ -505,22 +504,22 @@ void OnCollisionEnter2D(Collision2D other)

// --- ClientRPCs ---

[ClientRpc]
void PlayFireSoundClientRpc()
[Rpc(SendTo.ClientsAndHost)]
void ClientPlayFireSoundRpc()
{
fireSound.Play();
}
// --- ServerRPCs ---

[ServerRpc]
public void ThrustServerRpc(float thrusting, int spin)
[Rpc(SendTo.Server)]
public void ServerThrustRpc(float thrusting, int spin)
{
m_Thrust = thrusting;
m_Spin = spin;
}

[ServerRpc]
public void FireServerRpc()
[Rpc(SendTo.Server)]
public void ServerFireRpc()
{
if (Energy.Value >= 10)
{
Expand Down Expand Up @@ -549,8 +548,8 @@ public void FireServerRpc()
}
}

[ServerRpc]
public void SetNameServerRpc(string name)
[Rpc(SendTo.Server)]
public void ServerSetNameRpc(FixedString32Bytes name)
{
PlayerName.Value = name;
}
Expand Down
2 changes: 1 addition & 1 deletion Basic/2DSpaceShooter/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"com.unity.ide.visualstudio": "2.0.22",
"com.unity.ide.vscode": "1.2.5",
"com.unity.multiplayer.samples.coop": "https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop.git?path=/Packages/com.unity.multiplayer.samples.coop#v2.4.0",
"com.unity.netcode.gameobjects": "1.7.1",
"com.unity.netcode.gameobjects": "1.8.1",
"com.unity.postprocessing": "3.2.2",
"com.unity.render-pipelines.universal": "14.0.9",
"com.unity.test-framework": "1.1.33",
Expand Down
2 changes: 1 addition & 1 deletion Basic/2DSpaceShooter/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"url": "https://packages.unity.com"
},
"com.unity.netcode.gameobjects": {
"version": "1.7.1",
"version": "1.8.1",
"depth": 0,
"source": "registry",
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
- upgraded to com.unity.services.qos v1.3.0
- upgraded to com.unity.transport v1.4.1
- upgraded to com.unity.services.core v1.12.5
- Upgraded to Netcode for GameObjects v1.8.1 (#174)
- Upgraded to the newer API for Rpcs, Universal Rpcs
- Upgraded to newer API for Connection Events, OnConnectionEvent

#### Fixed
- Reset values and buffs after respawn of ship (#167)
Expand Down