Skip to content

Commit aaa545a

Browse files
committed
Refactor ammunition spawning to use networked RPC
Replaced local instantiation of ammunition with a networked RPC call (RPC_SpawnAmmunition) to ensure proper synchronization across clients. Updated ammunition classes to use networked owner/team IDs, improved collision handling, and added an AmmunitionDatabase asset. Refactored vehicle targeting to use NetworkId and added related synchronization logic. Minor adjustments to building, hangar, and material files for consistency.
1 parent 89119ff commit aaa545a

File tree

24 files changed

+274
-96
lines changed

24 files changed

+274
-96
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
m_GameObject: {fileID: 0}
10+
m_Enabled: 1
11+
m_EditorHideFlags: 0
12+
m_Script: {fileID: 11500000, guid: fc75f4bbc4db0be45b77ee35b0791436, type: 3}
13+
m_Name: Ammunition Database
14+
m_EditorClassIdentifier:
15+
ammunitions:
16+
- {fileID: 11400000, guid: 22882af161684a743a3776628e0fe427, type: 2}
17+
- {fileID: 11400000, guid: 5ef71d70c67b5bc4d964f8e71bd005ea, type: 2}
18+
- {fileID: 11400000, guid: 7199de5af23451c4ca4d2919b6b44983, type: 2}

Red Strike/Assets/AmmunitionSystem/Ammunition Database.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using UnityEngine;
22
using VehicleSystem.Vehicles;
33
using Fusion;
4-
using System.Security.Cryptography;
54

65
namespace AmmunitionSystem.Ammunitions
76
{
@@ -10,17 +9,11 @@ namespace AmmunitionSystem.Ammunitions
109
public class Ammunition : NetworkBehaviour
1110
{
1211
public AmmunitionSystem.Ammunition ammunitionData;
13-
public Vehicle ownerVehicle;
14-
public virtual void SetRocket(Transform targetTransform) { }
1512

16-
protected virtual void Start()
17-
{
18-
Invoke(nameof(OnDestroy), ammunitionData.lifetime);
19-
}
13+
[Networked] public int OwnerTeamId { get; set; } = -1;
14+
15+
[Networked] public NetworkId OwnerVehicleId { get; set; }
2016

21-
protected virtual void OnDestroy()
22-
{
23-
Runner.Despawn(Object);
24-
}
17+
public virtual void SetRocket(Transform targetTransform) { }
2518
}
26-
}
19+
}
Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using Fusion;
23

34
namespace AmmunitionSystem.Ammunitions.BasicBullet
45
{
@@ -10,29 +11,56 @@ public class BasicBullet : Ammunition
1011
private void Awake()
1112
{
1213
rb = GetComponent<Rigidbody>();
14+
rb.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
1315
}
1416

15-
protected override void Start()
17+
public override void Spawned()
1618
{
17-
base.Start();
1819
rb.linearVelocity = transform.forward * ammunitionData.speed;
20+
21+
if (Object.HasStateAuthority)
22+
{
23+
Invoke(nameof(DespawnBullet), ammunitionData.lifetime);
24+
}
1925
}
2026

2127
private void OnCollisionEnter(Collision collision)
2228
{
23-
if (ownerVehicle != null && collision.gameObject == ownerVehicle.gameObject)
24-
return;
29+
if (!Object.HasStateAuthority) return;
2530

26-
var unit = collision.gameObject.GetComponent<Unit.Unit>();
27-
if (unit == null)
28-
return;
31+
var hitNetObj = collision.gameObject.GetComponentInParent<NetworkObject>();
32+
33+
if (hitNetObj != null)
34+
{
35+
if (hitNetObj.Id == OwnerVehicleId)
36+
{
37+
return;
38+
}
39+
}
2940

30-
if (unit.teamId == ownerVehicle.teamId)
31-
return;
41+
var unit = collision.gameObject.GetComponentInParent<Unit.Unit>();
3242

33-
unit.TakeDamage(ammunitionData.damage);
43+
if (unit != null)
44+
{
45+
if (unit.teamId == OwnerTeamId)
46+
{
47+
DespawnBullet();
48+
return;
49+
}
3450

35-
OnDestroy();
51+
Debug.Log($"Hit Enemy: {unit.name} - Damage: {ammunitionData.damage}");
52+
unit.TakeDamage(ammunitionData.damage);
53+
}
54+
55+
DespawnBullet();
56+
}
57+
58+
private void DespawnBullet()
59+
{
60+
if (Object != null && Object.IsValid)
61+
{
62+
Runner.Despawn(Object);
63+
}
3664
}
3765
}
3866
}

Red Strike/Assets/AmmunitionSystem/Ammunitions/Ammunitions/BasicRocket/BasicRocket.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using Fusion;
23

34
namespace AmmunitionSystem.Ammunitions.BasicRocket
45
{
@@ -48,17 +49,24 @@ private void FixedUpdate()
4849

4950
private void OnCollisionEnter(Collision collision)
5051
{
52+
if (Object.HasStateAuthority == false) return;
53+
5154
if (hasExploded) return;
5255

53-
if (ownerVehicle != null && collision.gameObject == ownerVehicle.gameObject)
54-
return;
56+
if (collision.gameObject.TryGetComponent<NetworkObject>(out var netObj))
57+
{
58+
if (netObj.Id == OwnerVehicleId) return;
59+
}
5560

5661
var unit = collision.gameObject.GetComponent<Unit.Unit>();
5762
if (unit == null)
5863
return;
5964

60-
if (unit.teamId == ownerVehicle.teamId)
65+
if (unit.teamId == OwnerTeamId)
66+
{
67+
DespawnBullet();
6168
return;
69+
}
6270

6371
Debug.Log($"Hit unit: {collision.gameObject.name}, Damage: {ammunitionData.damage}");
6472
unit.TakeDamage(ammunitionData.damage);
@@ -71,7 +79,15 @@ private void OnCollisionEnter(Collision collision)
7179
Destroy(explosionEffect, 2f);
7280
}
7381

74-
OnDestroy();
82+
DespawnBullet();
83+
}
84+
85+
private void DespawnBullet()
86+
{
87+
if (Object != null && Object.IsValid)
88+
{
89+
Runner.Despawn(Object);
90+
}
7591
}
7692
}
7793
}

Red Strike/Assets/AmmunitionSystem/Ammunitions/Ammunitions/BasicTankShell/BasicTankShell.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using Fusion;
23

34
namespace AmmunitionSystem.Ammunitions.Ammunitions.BasicTankShell
45
{
@@ -14,25 +15,35 @@ private void Awake()
1415
rb = GetComponent<Rigidbody>();
1516
}
1617

17-
protected override void Start()
18+
private void Start()
1819
{
19-
base.Start();
2020
rb.linearVelocity = transform.forward * ammunitionData.speed;
21+
22+
if (Object.HasStateAuthority)
23+
{
24+
Invoke(nameof(DespawnBullet), ammunitionData.lifetime);
25+
}
2126
}
2227

2328
private void OnCollisionEnter(Collision collision)
2429
{
30+
if (Object.HasStateAuthority == false) return;
31+
2532
if (hasExploded) return;
2633

27-
if (ownerVehicle != null && collision.gameObject == ownerVehicle.gameObject)
28-
return;
34+
if (collision.gameObject.TryGetComponent<NetworkObject>(out var netObj))
35+
{
36+
if (netObj.Id == OwnerVehicleId) return;
37+
}
2938

3039
var unit = collision.gameObject.GetComponent<Unit.Unit>();
31-
if (unit == null)
32-
return;
40+
if (unit == null) return;
3341

34-
if (unit.teamId == ownerVehicle.teamId)
42+
if (unit.teamId == OwnerTeamId)
43+
{
44+
DespawnBullet();
3545
return;
46+
}
3647

3748
Debug.Log($"Hit unit: {collision.gameObject.name}, Damage: {ammunitionData.damage}");
3849
unit.TakeDamage(ammunitionData.damage);
@@ -45,7 +56,15 @@ private void OnCollisionEnter(Collision collision)
4556
Destroy(explosionEffect, 2f);
4657
}
4758

48-
Runner.Despawn(Object);
59+
DespawnBullet();
60+
}
61+
62+
private void DespawnBullet()
63+
{
64+
if (Object != null && Object.IsValid)
65+
{
66+
Runner.Despawn(Object);
67+
}
4968
}
5069
}
5170
}

Red Strike/Assets/BuildingPlacement/Buildings/Building.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ public override void Spawned()
1919
if (Object.HasStateAuthority)
2020
{
2121
health = buildingData.maxHealth;
22-
Invoke(nameof(Test_Damage), 5f);
23-
}
24-
}
25-
26-
private void Test_Damage()
27-
{
28-
if (Object.HasStateAuthority)
29-
{
30-
TakeDamage(health + 1);
3122
}
3223
}
3324

Red Strike/Assets/BuildingPlacement/Buildings/Hangar/Hangar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Hangar : Building
1616

1717
private void Start()
1818
{
19-
vehicleSpawnPoint = transform.position + transform.forward * 10f;
19+
vehicleSpawnPoint = transform.position + transform.forward * 20f;
2020
}
2121

2222
public void CreateVehicle(VehicleTypes vehicleType)

Red Strike/Assets/InputController/InputController.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using UnityEngine.UIElements;
88
using NetworkingSystem;
9+
using AmmunitionSystem;
910

1011
namespace InputController
1112
{
@@ -17,6 +18,7 @@ public class InputController : MonoBehaviour
1718
public UIDocument gameUIDocument;
1819
public BuildingsDatabase buildingsDatabase;
1920
public VehiclesDatabase vehiclesDatabase;
21+
public AmmunitionDatabase ammunitionDatabase;
2022

2123
[Header("Layers & Camera")]
2224
public LayerMask terrainLayer;

Red Strike/Assets/NetworkingSystem/CommanderData.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ private void OnTeamIdChanged()
6868
[Rpc(RpcSources.InputAuthority, RpcTargets.StateAuthority)]
6969
public void RPC_SpawnBuilding(string buildingName, Vector3 position)
7070
{
71-
Debug.Log($"Server: {Object.InputAuthority} oyuncusu {buildingName} kurmak istiyor.");
72-
7371
if (InputController.InputController.Instance == null) return;
7472

7573
var database = InputController.InputController.Instance.buildingsDatabase;
@@ -95,8 +93,6 @@ public void RPC_SpawnBuilding(string buildingName, Vector3 position)
9593
[Rpc(RpcSources.InputAuthority, RpcTargets.StateAuthority)]
9694
public void RPC_SpawnVehicle(string vehicleName, Vector3 position)
9795
{
98-
Debug.Log($"Server: {Object.InputAuthority} oyuncusu {vehicleName} üretmek istiyor.");
99-
10096
if (InputController.InputController.Instance == null) return;
10197

10298
var database = InputController.InputController.Instance.vehiclesDatabase;
@@ -119,6 +115,37 @@ public void RPC_SpawnVehicle(string vehicleName, Vector3 position)
119115
}
120116
}
121117

118+
119+
[Rpc(RpcSources.InputAuthority, RpcTargets.StateAuthority)]
120+
public void RPC_SpawnAmmunition(string ammunitionName, Vector3 position, Quaternion rotation, NetworkObject ownerVehicleNetObj)
121+
{
122+
if (Runner == null || !Runner.IsServer) return;
123+
124+
if (InputController.InputController.Instance == null) return;
125+
126+
var database = InputController.InputController.Instance.ammunitionDatabase;
127+
var ammunitionData = database.ammunitions.FirstOrDefault(a => a.ammunitionName == ammunitionName);
128+
129+
if (ammunitionData != null && ammunitionData.ammunitionPrefab != null)
130+
{
131+
NetworkObject spawnedObj = Runner.Spawn(ammunitionData.ammunitionPrefab, position, rotation, Object.InputAuthority);
132+
var ammunitionScript = spawnedObj.GetComponent<AmmunitionSystem.Ammunitions.Ammunition>();
133+
var vehicleScript = ownerVehicleNetObj.GetComponent<VehicleSystem.Vehicles.Vehicle>();
134+
135+
if (ammunitionScript != null && vehicleScript != null)
136+
{
137+
ammunitionScript.OwnerTeamId = vehicleScript.teamId;
138+
ammunitionScript.OwnerVehicleId = ownerVehicleNetObj.Id;
139+
140+
Debug.Log($"Mühimmat fırlatıldı: {ammunitionName}. Takım: {vehicleScript.teamId}");
141+
}
142+
}
143+
else
144+
{
145+
Debug.LogError($"Server: {ammunitionName} prefabı bulunamadı!");
146+
}
147+
}
148+
122149
public void OnDisconnect()
123150
{
124151
if (Runner != null && Runner.IsRunning)

0 commit comments

Comments
 (0)