Skip to content

Commit 63fa238

Browse files
committed
Enhance vehicle selection and targeting logic
Refactored InputController to support selecting vehicles and sending them to attack enemies. Improved vehicle selection highlighting and UI updates. Updated GroundVehicle and Vehicle classes to better handle movement, fuel, and attack logic, including a new SetTargetEnemy method and improved smoke effect handling. Scene and NavMesh asset changes reflect these logic updates.
1 parent ff362a3 commit 63fa238

File tree

5 files changed

+104
-76
lines changed

5 files changed

+104
-76
lines changed

Red Strike/Assets/InputController/InputController.cs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class InputController : MonoBehaviour
1818
private Camera mainCamera;
1919
public BuildingsDatabase buildingsDatabase;
2020
private Dictionary<string, int> buildingCounts = new Dictionary<string, int>();
21-
private Building selectedBuilding;
21+
private Building currentSelectedBuilding;
22+
private Vehicle currentSelectedVehicle;
2223
private List<GameObject> placedObjects = new List<GameObject>();
2324
public float minDistanceBetweenObjects = 5f;
2425
public VehiclesHUDController vehiclesHUDController;
@@ -33,10 +34,9 @@ private void Start()
3334

3435
private void Update()
3536
{
36-
if (Input.GetMouseButtonDown(1) && selectedBuilding != null)
37+
if (Input.GetMouseButtonDown(1) && currentSelectedBuilding != null)
3738
{
38-
Debug.Log("Bina yerleştirme iptal edildi.");
39-
selectedBuilding = null;
39+
DeselectAll();
4040
return;
4141
}
4242

@@ -47,7 +47,7 @@ private void Update()
4747
return;
4848
}
4949

50-
if (selectedBuilding != null)
50+
if (currentSelectedBuilding != null)
5151
{
5252
PlaceBuilding();
5353
}
@@ -84,25 +84,25 @@ private void PlaceBuilding()
8484

8585
if (IsPositionValid(spawnPosition))
8686
{
87-
if (buildingCounts.ContainsKey(selectedBuilding.buildingName) &&
88-
buildingCounts[selectedBuilding.buildingName] >= selectedBuilding.maxCreatedUnits)
87+
if (buildingCounts.ContainsKey(currentSelectedBuilding.buildingName) &&
88+
buildingCounts[currentSelectedBuilding.buildingName] >= currentSelectedBuilding.maxCreatedUnits)
8989
{
90-
Debug.Log(selectedBuilding.buildingName + " için maksimum yerleştirme limitine ulaşıldı.");
90+
Debug.Log(currentSelectedBuilding.buildingName + " için maksimum yerleştirme limitine ulaşıldı.");
9191
return;
9292
}
93-
GameObject placedObject = Instantiate(selectedBuilding.buildingPrefab, spawnPosition, Quaternion.identity);
93+
GameObject placedObject = Instantiate(currentSelectedBuilding.buildingPrefab, spawnPosition, Quaternion.identity);
9494

9595
if (placedObject.GetComponent<SelectionHighlighter>() == null)
9696
placedObject.AddComponent<SelectionHighlighter>();
9797

9898
placedObjects.Add(placedObject);
9999

100-
if (buildingCounts.ContainsKey(selectedBuilding.buildingName))
101-
buildingCounts[selectedBuilding.buildingName]++;
100+
if (buildingCounts.ContainsKey(currentSelectedBuilding.buildingName))
101+
buildingCounts[currentSelectedBuilding.buildingName]++;
102102
else
103-
buildingCounts[selectedBuilding.buildingName] = 1;
103+
buildingCounts[currentSelectedBuilding.buildingName] = 1;
104104

105-
selectedBuilding = null;
105+
currentSelectedBuilding = null;
106106
Debug.Log(placedObject.name + " yerleştirildi.");
107107
}
108108
else
@@ -118,8 +118,8 @@ public void SelectBuildingToPlace(string buildingName)
118118

119119
if (buildingToSelect != null)
120120
{
121-
selectedBuilding = buildingToSelect;
122-
Debug.Log("Seçilen bina: " + selectedBuilding.buildingName + ". Yerleştirmek için araziye tıklayın.");
121+
currentSelectedBuilding = buildingToSelect;
122+
Debug.Log("Seçilen bina: " + currentSelectedBuilding.buildingName + ". Yerleştirmek için araziye tıklayın.");
123123
}
124124
else
125125
{
@@ -134,31 +134,40 @@ private void SelectObject()
134134

135135
if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity, selectableLayer))
136136
{
137-
DeselectAll();
137+
GameObject hitObject = hitInfo.collider.gameObject;
138+
139+
if (currentSelectedVehicle != null && hitObject.CompareTag("Enemy"))
140+
{
141+
Debug.Log($"Araç ({currentSelectedVehicle.name}) hedefe ({hitObject.name}) gönderiliyor.");
142+
currentSelectedVehicle.SetTargetEnemy(hitObject);
143+
DeselectAll();
138144

139-
currentSelectionHighlighter = hitInfo.collider.GetComponent<SelectionHighlighter>();
145+
return;
146+
}
140147

148+
DeselectAll();
141149

142-
if (currentSelectionHighlighter != null)
143-
currentSelectionHighlighter.EnableHighlight();
150+
currentSelectionHighlighter = hitObject.GetComponent<SelectionHighlighter>();
151+
if (currentSelectionHighlighter == null) currentSelectionHighlighter = hitObject.GetComponentInParent<SelectionHighlighter>();
144152

145-
switch (hitInfo.collider.tag)
153+
switch (hitObject.tag)
146154
{
147-
case "Build":
148-
BuildingPlacement.Buildings.Building clickedBuilding = hitInfo.collider.GetComponent<BuildingPlacement.Buildings.Building>();
155+
case "Vehicle":
156+
if (currentSelectionHighlighter != null) currentSelectionHighlighter.EnableHighlight();
149157

150-
if (clickedBuilding != null)
158+
Vehicle v = hitObject.GetComponent<Vehicle>();
159+
if (v != null)
151160
{
152-
buildingHUDController.ShowBuildingDetails(clickedBuilding);
153-
if (vehiclesHUDController != null) vehiclesHUDController.HideVehicleDetails();
161+
currentSelectedVehicle = v;
162+
if (vehiclesHUDController != null) vehiclesHUDController.ShowVehicleDetails(v);
154163
}
155164
break;
156165

157-
case "Vehicle":
158-
Vehicle clickedVehicle = hitInfo.collider.GetComponent<Vehicle>();
166+
case "Build":
167+
if (currentSelectionHighlighter != null) currentSelectionHighlighter.EnableHighlight();
159168

160-
if (vehiclesHUDController != null) vehiclesHUDController.ShowVehicleDetails(clickedVehicle);
161-
buildingHUDController.HideBuildingDetails();
169+
var b = hitObject.GetComponent<BuildingPlacement.Buildings.Building>();
170+
if (b != null) buildingHUDController.ShowBuildingDetails(b);
162171
break;
163172

164173
default:
@@ -174,14 +183,20 @@ private void SelectObject()
174183

175184
private void DeselectAll()
176185
{
186+
// UI Gizle
177187
if (vehiclesHUDController != null) vehiclesHUDController.HideVehicleDetails();
178-
buildingHUDController.HideBuildingDetails();
188+
if (buildingHUDController != null) buildingHUDController.HideBuildingDetails();
179189

190+
// Outline Kapat
180191
if (currentSelectionHighlighter != null)
181192
{
182193
currentSelectionHighlighter.DisableHighlight();
183194
currentSelectionHighlighter = null;
184195
}
196+
197+
// Hafızayı temizle
198+
currentSelectedVehicle = null;
199+
currentSelectedBuilding = null;
185200
}
186201

187202
private bool IsPositionValid(Vector3 position)

Red Strike/Assets/Scenes/SampleScene.unity

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,10 @@ PrefabInstance:
890890
propertyPath: m_Name
891891
value: Quad
892892
objectReference: {fileID: 0}
893+
- target: {fileID: 1855602119919018044, guid: 16678156d05891545a8ea7f1b6b4e7f6, type: 3}
894+
propertyPath: m_IsActive
895+
value: 1
896+
objectReference: {fileID: 0}
893897
- target: {fileID: 5052494845539565231, guid: 16678156d05891545a8ea7f1b6b4e7f6, type: 3}
894898
propertyPath: targetObject
895899
value:
@@ -1012,7 +1016,7 @@ Transform:
10121016
m_GameObject: {fileID: 1702380761}
10131017
serializedVersion: 2
10141018
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
1015-
m_LocalPosition: {x: 7.0737457, y: -10.843618, z: -5.863739}
1019+
m_LocalPosition: {x: 0, y: 0, z: 0}
10161020
m_LocalScale: {x: 1, y: 1, z: 1}
10171021
m_ConstrainProportionsScale: 0
10181022
m_Children: []
-928 Bytes
Binary file not shown.

Red Strike/Assets/VehicleSystem/Vehicles/GroundVehicle.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,57 @@ protected override void Start()
2222

2323
protected override void Update()
2424
{
25-
base.Update();
26-
27-
if (targetObject == null)
25+
if (fuelLevel <= 0)
2826
{
29-
if (agent.hasPath)
30-
{
31-
agent.isStopped = true;
32-
agent.ResetPath();
33-
}
27+
fuelLevel = 0;
28+
if (agent.enabled) agent.isStopped = true;
3429
StopAttacking();
3530
isMoving = false;
3631
return;
3732
}
3833

39-
if (fuelLevel <= 0)
34+
if (targetObject == null)
4035
{
41-
fuelLevel = 0;
42-
agent.isStopped = true;
36+
if (agent.enabled && agent.hasPath)
37+
{
38+
agent.isStopped = true;
39+
agent.ResetPath();
40+
}
4341
StopAttacking();
4442
isMoving = false;
4543
return;
4644
}
4745

46+
isMoving = true;
4847
LookAtTarget();
4948

50-
agent.SetDestination(targetObject.transform.position);
51-
52-
if (agent.remainingDistance <= agent.stoppingDistance)
49+
if (agent.enabled)
5350
{
54-
isMoving = false;
51+
agent.isStopped = false;
52+
agent.SetDestination(targetObject.transform.position);
5553

56-
if (!isAttacking)
54+
if (agent.remainingDistance <= agent.stoppingDistance && !agent.pathPending)
5755
{
58-
StartAttacking();
56+
if (!isAttacking) StartAttacking();
57+
}
58+
else
59+
{
60+
StopAttacking();
61+
ConsumeFuel();
5962
}
60-
}
61-
else
62-
{
63-
agent.isStopped = false;
64-
isMoving = true;
65-
StopAttacking();
66-
67-
ConsumeFuel();
6863
}
6964

7065
UpdateSmokeEffect();
7166
}
67+
68+
protected override void UpdateSmokeEffect()
69+
{
70+
if (smokeEffect == null) return;
71+
72+
bool actuallyMoving = agent.velocity.sqrMagnitude > 0.1f && isMoving;
73+
74+
if (actuallyMoving && !smokeEffect.isPlaying) smokeEffect.Play();
75+
else if (!actuallyMoving && smokeEffect.isPlaying) smokeEffect.Stop();
76+
}
7277
}
7378
}

Red Strike/Assets/VehicleSystem/Vehicles/Vehicle.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,41 @@ namespace VehicleSystem.Vehicles
88
public class Vehicle : MonoBehaviour
99
{
1010
public VehicleSystem.Vehicle vehicleData;
11+
12+
public GameObject targetObject;
13+
public ParticleSystem smokeEffect;
14+
1115
protected float speed;
12-
protected float coverageAreaRadius;
1316
protected float turnSpeed;
1417
protected float fuelLevel;
1518
protected float fuelConsumptionRate;
1619
protected float health;
17-
private float maxHealth = 100f;
18-
private float stoppingDistance = 1.5f;
20+
protected float maxHealth;
21+
protected float stoppingDistance;
22+
1923
protected int maxAmmunition = 30;
2024
protected int currentAmmunition = 30;
2125
protected int reloadCounter = 0;
2226
protected float bulletDamage = 10f;
2327
protected float bulletSpeed = 20f;
2428
protected float reloadTime = 1.5f;
25-
2629
protected GameObject bulletPrefab;
2730

28-
public GameObject targetObject;
29-
30-
public ParticleSystem smokeEffect;
31-
32-
private NavMeshAgent agent;
33-
3431
protected bool isMoving = false;
3532
protected bool isAttacking = false;
36-
3733
private Coroutine attackCoroutine;
3834

3935
protected virtual void Start()
4036
{
4137
Setup();
42-
agent = GetComponent<NavMeshAgent>();
43-
agent.speed = speed;
44-
agent.stoppingDistance = stoppingDistance;
4538
}
4639

4740
private void Setup()
4841
{
4942
speed = vehicleData.speed;
50-
coverageAreaRadius = vehicleData.coverageAreaRadius;
5143
turnSpeed = vehicleData.turnSpeed;
5244
fuelLevel = vehicleData.fuelCapacity;
5345
fuelConsumptionRate = vehicleData.fuelConsumptionRate;
54-
stoppingDistance = vehicleData.stoppingDistance;
5546
maxAmmunition = vehicleData.maxAmmunition;
5647
currentAmmunition = maxAmmunition;
5748
bulletDamage = vehicleData.bulletDamage;
@@ -60,13 +51,22 @@ private void Setup()
6051
bulletPrefab = vehicleData.bulletPrefab;
6152
maxHealth = vehicleData.maxHealth;
6253
health = maxHealth;
54+
stoppingDistance = vehicleData.stoppingDistance;
6355
}
6456

6557
public (string, float, int, int, float) GetVehicleStatus()
6658
{
6759
return (vehicleData.vehicleName, fuelLevel, currentAmmunition, maxAmmunition, health);
6860
}
6961

62+
public virtual void SetTargetEnemy(GameObject enemy)
63+
{
64+
if (fuelLevel <= 0) return;
65+
66+
targetObject = enemy;
67+
isMoving = true;
68+
}
69+
7070
protected virtual void Update()
7171
{
7272
}
@@ -82,10 +82,14 @@ protected virtual void ConsumeFuel()
8282

8383
protected virtual void LookAtTarget()
8484
{
85-
Vector3 direction = (targetObject.transform.position - transform.position).normalized;
86-
direction.y = 0;
87-
Quaternion lookRotation = Quaternion.LookRotation(direction);
88-
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, agent.angularSpeed * Time.deltaTime);
85+
if (targetObject == null) return;
86+
Vector3 direction = (targetObject.transform.position - transform.position).normalized;
87+
direction.y = 0;
88+
if (direction != Vector3.zero)
89+
{
90+
Quaternion lookRotation = Quaternion.LookRotation(direction);
91+
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
92+
}
8993
}
9094

9195
protected virtual void UpdateSmokeEffect()

0 commit comments

Comments
 (0)