Skip to content

Commit 56cf38c

Browse files
committed
Add networked rocket targeting and explosion effects
Introduces networked rocket targeting by passing NetworkId to rockets and synchronizing their movement. Explosion effects for rockets are now spawned via RPC on the server. AirVehicle movement is now networked for smooth multiplayer synchronization. GameHUD adds more vehicle creation buttons. Minor material and fuel consumption adjustments.
1 parent 9daac6b commit 56cf38c

File tree

13 files changed

+138
-68
lines changed

13 files changed

+138
-68
lines changed

Red Strike/Assets/AmmunitionSystem/Ammunition.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public class Ammunition : ScriptableObject
1212
public float range;
1313
public float lifetime;
1414
public GameObject ammunitionPrefab;
15+
public GameObject explosionEffectPrefab;
1516
}
1617
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using UnityEngine;
2-
using VehicleSystem.Vehicles;
32
using Fusion;
43

54
namespace AmmunitionSystem.Ammunitions
@@ -11,9 +10,8 @@ public class Ammunition : NetworkBehaviour
1110
public AmmunitionSystem.Ammunition ammunitionData;
1211

1312
[Networked] public int OwnerTeamId { get; set; } = -1;
14-
1513
[Networked] public NetworkId OwnerVehicleId { get; set; }
1614

17-
public virtual void SetRocket(Transform targetTransform) { }
15+
public virtual void SetRocketTarget(NetworkId targetId) { }
1816
}
1917
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ MonoBehaviour:
1919
range: -1
2020
lifetime: 10
2121
ammunitionPrefab: {fileID: 595683329615967435, guid: ab318667f38c4db4ebc5472f6d16eed0, type: 3}
22+
explosionEffectPrefab: {fileID: 100012, guid: 3364c97de68d3a147af572d1c71c6455, type: 3}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
11
using UnityEngine;
22
using Fusion;
3+
using NetworkingSystem;
34

45
namespace AmmunitionSystem.Ammunitions.BasicRocket
56
{
67
[RequireComponent(typeof(Rigidbody))]
78
public class BasicRocket : Ammunition
89
{
910
private Rigidbody rb;
10-
public GameObject explosionEffectPrefab;
11-
public Transform target;
1211
public float speed = 20f;
1312
public float rotationSpeed = 5f;
1413
public float accelerationRate = 1.5f;
1514

1615
private bool hasExploded = false;
1716
private float currentSpeed;
17+
[Networked] public NetworkId TargetId { get; set; }
1818

1919
private void Awake()
2020
{
2121
rb = GetComponent<Rigidbody>();
2222
currentSpeed = speed * 0.5f;
2323
}
2424

25-
public override void SetRocket(Transform targetTransform)
25+
public override void SetRocketTarget(NetworkId targetId)
2626
{
27-
target = targetTransform;
27+
TargetId = targetId;
2828
}
2929

30-
private void FixedUpdate()
30+
public override void FixedUpdateNetwork()
3131
{
32+
if (!Object.HasStateAuthority) return;
3233
if (hasExploded) return;
3334

34-
currentSpeed = Mathf.Lerp(currentSpeed, speed, Time.fixedDeltaTime * accelerationRate);
35+
currentSpeed = Mathf.Lerp(currentSpeed, speed, Runner.DeltaTime * accelerationRate);
3536

36-
if (target != null)
37+
if (TargetId.IsValid)
3738
{
38-
Vector3 direction = (target.position - transform.position).normalized;
39-
Quaternion targetRotation = Quaternion.LookRotation(direction);
40-
rb.MoveRotation(Quaternion.Slerp(rb.rotation, targetRotation, Time.fixedDeltaTime * rotationSpeed));
41-
42-
rb.linearVelocity = transform.forward * currentSpeed;
43-
}
44-
else
45-
{
46-
rb.linearVelocity = transform.forward * currentSpeed;
39+
var targetObj = Runner.FindObject(TargetId);
40+
41+
if (targetObj != null)
42+
{
43+
Vector3 direction = (targetObj.transform.position - transform.position).normalized;
44+
if (direction != Vector3.zero)
45+
{
46+
Quaternion targetRotation = Quaternion.LookRotation(direction);
47+
rb.MoveRotation(Quaternion.Slerp(rb.rotation, targetRotation, Runner.DeltaTime * rotationSpeed));
48+
}
49+
}
4750
}
51+
52+
rb.linearVelocity = transform.forward * currentSpeed;
4853
}
4954

5055
private void OnCollisionEnter(Collision collision)
5156
{
52-
if (Object.HasStateAuthority == false) return;
53-
57+
if (!Object.HasStateAuthority) return;
5458
if (hasExploded) return;
5559

5660
if (collision.gameObject.TryGetComponent<NetworkObject>(out var netObj))
@@ -72,12 +76,7 @@ private void OnCollisionEnter(Collision collision)
7276
unit.TakeDamage(ammunitionData.damage);
7377

7478
hasExploded = true;
75-
76-
if (explosionEffectPrefab != null)
77-
{
78-
GameObject explosionEffect = Instantiate(explosionEffectPrefab, transform.position, Quaternion.identity);
79-
Destroy(explosionEffect, 2f);
80-
}
79+
CommanderData.LocalCommander.RPC_SpawnExplosionEffect(transform.position);
8180

8281
DespawnBullet();
8382
}

Red Strike/Assets/InputController/InputController.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using UnityEngine.UIElements;
88
using NetworkingSystem;
99
using AmmunitionSystem;
10-
using Fusion;
1110

1211
namespace InputController
1312
{
@@ -40,8 +39,7 @@ public class InputController : MonoBehaviour
4039

4140
private SelectionHighlighter targetHighlighter;
4241
private SelectionHighlighter tempBuildingHighlighter;
43-
44-
// Limitleme için sayaç (İsteğe bağlı, şimdilik basit tutuyoruz)
42+
4543
private Dictionary<string, int> buildingCounts = new Dictionary<string, int>();
4644

4745
private void Awake()
@@ -77,16 +75,12 @@ private void Update()
7775
{
7876
if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKeyDown(KeyCode.R))
7977
{
80-
// Highlight edilen objenin Building scriptini bul
8178
var building = tempBuildingHighlighter.GetComponent<BuildingPlacement.Buildings.Building>();
8279

83-
// Eğer script o objede değilse parent'a bak
8480
if (building == null) building = tempBuildingHighlighter.GetComponentInParent<BuildingPlacement.Buildings.Building>();
8581

8682
if (building != null)
8783
{
88-
Debug.Log("Dönme isteği gönderiliyor...");
89-
// 2. RPC'Yİ BURADAN ÇAĞIRIN
9084
building.RPC_Rotate90();
9185
}
9286
}
@@ -264,7 +258,7 @@ private SelectionHighlighter GetHighlighter(GameObject obj)
264258
if (hl == null) hl = obj.GetComponentInParent<SelectionHighlighter>();
265259
return hl;
266260
}
267-
261+
268262
private bool IsPointerOverUI()
269263
{
270264
if (gameUIDocument == null) return false;

Red Strike/Assets/NetworkingSystem/CommanderData.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,12 @@ public void RPC_SpawnVehicle(string vehicleName, Vector3 position)
117117

118118

119119
[Rpc(RpcSources.InputAuthority, RpcTargets.StateAuthority)]
120-
public void RPC_SpawnAmmunition(string ammunitionName, Vector3 position, Quaternion rotation, NetworkObject ownerVehicleNetObj)
120+
public void RPC_SpawnAmmunition(
121+
string ammunitionName,
122+
Vector3 position,
123+
Quaternion rotation,
124+
NetworkObject ownerVehicleNetObj,
125+
NetworkId targetId = default)
121126
{
122127
if (Runner == null || !Runner.IsServer) return;
123128

@@ -137,12 +142,30 @@ public void RPC_SpawnAmmunition(string ammunitionName, Vector3 position, Quatern
137142
ammunitionScript.OwnerTeamId = vehicleScript.teamId;
138143
ammunitionScript.OwnerVehicleId = ownerVehicleNetObj.Id;
139144

145+
if (ammunitionData.ammunitionType == AmmunitionSystem.AmmunitionType.Rocket && targetId.IsValid)
146+
{
147+
ammunitionScript.SetRocketTarget(targetId);
148+
}
149+
140150
Debug.Log($"Mühimmat fırlatıldı: {ammunitionName}. Takım: {vehicleScript.teamId}");
141151
}
142152
}
143-
else
153+
}
154+
155+
[Rpc(RpcSources.InputAuthority, RpcTargets.StateAuthority)]
156+
public void RPC_SpawnExplosionEffect(Vector3 position)
157+
{
158+
if (Runner == null || !Runner.IsServer) return;
159+
160+
if (InputController.InputController.Instance == null) return;
161+
162+
var explosionEffectPrefab = InputController.InputController.Instance.ammunitionDatabase.ammunitions.
163+
FirstOrDefault(e => e.ammunitionType == AmmunitionSystem.AmmunitionType.Rocket)?.explosionEffectPrefab;
164+
165+
if (explosionEffectPrefab != null)
144166
{
145-
Debug.LogError($"Server: {ammunitionName} prefabı bulunamadı!");
167+
Runner.Spawn(explosionEffectPrefab, position, Quaternion.identity, Object.InputAuthority);
168+
Debug.Log($"Patlama efekti oluşturuldu.");
146169
}
147170
}
148171

Red Strike/Assets/PlanetAtmosphereSystem/AtmosphereMaterial.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Material:
4747
- _GunesYonu: {r: -0.91, g: 0.4, b: 3.24, a: 0}
4848
- _RenkUzak: {r: 0, g: 0.10166844, b: 2.152374, a: 1}
4949
- _RenkYakin: {r: 0, g: 5.670685, b: 8, a: 1}
50-
- _SunDirection: {r: -0.16461955, g: 0.31825417, b: 0.93360317, a: 0}
50+
- _SunDirection: {r: -0.019243166, g: -0.9938409, b: 0.10913344, a: 0}
5151
m_BuildTextureStacks: []
5252
m_AllowLocking: 1
5353
--- !u!114 &1522106816477559216

Red Strike/Assets/PlanetAtmosphereSystem/CloudMaterial.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Material:
4343
m_Colors:
4444
- _Color: {r: 0, g: 3.8274493, b: 5.992155, a: 1}
4545
- _GunesYonu: {r: -1, g: 0, b: 0, a: 0}
46-
- _SunDirection: {r: -0.16461955, g: 0.31825417, b: 0.93360317, a: 0}
46+
- _SunDirection: {r: -0.019243166, g: -0.9938409, b: 0.10913344, a: 0}
4747
m_BuildTextureStacks: []
4848
m_AllowLocking: 1
4949
--- !u!114 &8499732345945835446

Red Strike/Assets/PlanetAtmosphereSystem/FogMaterial.mat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ Material:
5454
- _QueueControl: 0
5555
- _QueueOffset: 0
5656
m_Colors:
57-
- _SunDirection: {r: -0.16461955, g: 0.31825417, b: 0.93360317, a: 0}
57+
- _SunDirection: {r: -0.019243166, g: -0.9938409, b: 0.10913344, a: 0}
5858
m_BuildTextureStacks: []
5959
m_AllowLocking: 1

Red Strike/Assets/UISystem/GameHUD.uxml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@
3333
<ui:Label text="In production:" name="in-production-label" class="details-text" />
3434
<ui:ScrollView mode="Vertical" style="max-height: 300px;">
3535
<ui:Button text="Create Infantry" name="infantry-create-button" class="action-button" />
36-
<!-- Diğer butonlar... -->
36+
<ui:Button text="Create Trike" name="trike-create-button" class="action-button" />
37+
<ui:Button text="Create Quad" name="quad-create-button" class="action-button" />
38+
<ui:Button text="Create Tank Combat" name="tank-combat-create-button" class="action-button" />
39+
<ui:Button text="Create Tank Heavy A" name="tank-heavy-a-create-button" class="action-button" />
40+
<ui:Button text="Create Tank Heavy B" name="tank-heavy-b-create-button" class="action-button" />
41+
<ui:Button text="Create Ornithopter A" name="ornithopter-a-create-button" class="action-button" />
42+
<ui:Button text="Create Ornithopter B" name="ornithopter-b-create-button" class="action-button" />
3743
</ui:ScrollView>
3844
</ui:VisualElement>
3945
</ui:VisualElement>

0 commit comments

Comments
 (0)