-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShootAuto.cs
More file actions
125 lines (102 loc) · 4.54 KB
/
ShootAuto.cs
File metadata and controls
125 lines (102 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using UnityEngine;
public class ShootAuto : MonoBehaviour
{
[Header("Ball Settings")]
public Transform ballSpawnPoint; // The position and rotation where the ball will spawn
public Rigidbody ballPrefab; // The prefab for the ball
[SerializeField] private float spawnForce = 13000f; // Force applied to the ball when spawned
[Header("Timing Settings")]
[SerializeField] private float spawnTimeMin = 10f; // Minimum time before the first spawn
[SerializeField] private float spawnTimeMax = 15f; // Maximum time before the first spawn
[SerializeField] private float spawnIntervalMin = 3f; // Minimum interval between spawns
[SerializeField] private float spawnIntervalMax = 7f; // Maximum interval between spawns
[Header("Effects")]
public GameObject spawnEffect; // Optional effect to instantiate when the ball is spawned
private void Start()
{
ScheduleNextSpawn();
}
private void SpawnBall()
{
if (ballPrefab == null || ballSpawnPoint == null)
{
Debug.LogWarning("Ball or Spawn Point is not assigned. Please assign them in the Inspector.");
return;
}
// Instantiate the ball and add force
Rigidbody ballInstance = Instantiate(ballPrefab, ballSpawnPoint.position, ballSpawnPoint.rotation);
if (ballInstance != null)
{
ballInstance.AddForce(ballSpawnPoint.forward * spawnForce, ForceMode.Impulse);
}
else
{
Debug.LogWarning("Failed to instantiate ball. Please check the prefab.");
}
// Instantiate the effect if provided
if (spawnEffect != null)
{
Instantiate(spawnEffect, ballSpawnPoint.position, ballSpawnPoint.rotation);
}
// Schedule the next spawn
ScheduleNextSpawn();
}
private void ScheduleNextSpawn()
{
float timeToNextSpawn = Random.Range(spawnTimeMin, spawnTimeMax);
float interval = Random.Range(spawnIntervalMin, spawnIntervalMax);
Invoke("SpawnBall", timeToNextSpawn + interval);
}
/*
==================== TUTORIAL FOR DEVELOPERS ====================
This script automatically spawns a ball (or bullet) at regular intervals
and applies a force to it in the forward direction.
1. **Purpose:**
- Automatically spawns bullets/balls and applies a force for shooting functionality.
2. **Setup:**
- Attach this script to a GameObject (e.g., an empty GameObject in your scene).
- Assign the following in the Unity Inspector:
- `ballSpawnPoint`: A Transform that determines where and in what direction the ball is spawned.
- `ballPrefab`: A Rigidbody prefab representing the ball to be spawned.
- `spawnEffect` (optional): A GameObject prefab for an effect to show when the ball spawns.
3. **Timing Settings:**
- Adjust the following parameters in the Inspector:
- `spawnTimeMin` and `spawnTimeMax`: Control the random range for the time before the first spawn.
- `spawnIntervalMin` and `spawnIntervalMax`: Control the random range for the interval between subsequent spawns.
4. **Force Application:**
- The `spawnForce` determines how much force is applied to the ball when it spawns.
- The force is applied in the forward direction of the `ballSpawnPoint`.
5. **Effects (Optional):**
- Assign an effect prefab to `spawnEffect` (e.g., particle system or visual indicator) to show when the ball is spawned.
6. **Enhancements:**
- Integrate with a `Health` script (example below) for damage handling.
- Add sounds or effects for a more immersive experience.
=================================================================
*/
/* =================== Example Health Script ===================
using UnityEngine;
public class Health : MonoBehaviour
{
[SerializeField] private float maxHealth = 100f;
private float currentHealth;
private void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(float damage)
{
currentHealth -= damage;
Debug.Log($"{gameObject.name} took {damage} damage. Current health: {currentHealth}");
if (currentHealth <= 0)
{
Die();
}
}
private void Die()
{
Debug.Log($"{gameObject.name} has been destroyed.");
Destroy(gameObject);
}
}
================================================================ */
}