Added Car Alarm Super Surprise
This commit is contained in:
parent
8dfbd22a9e
commit
f737955a88
10 changed files with 1240 additions and 304 deletions
50
Assets/Scripts/CartSpawner.cs
Normal file
50
Assets/Scripts/CartSpawner.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class CartSpawner : MonoBehaviour
|
||||
{
|
||||
public GameObject cartPrefab;
|
||||
public Vector3 spawnAreaCenter;
|
||||
public Vector3 spawnAreaSize;
|
||||
public float spawnInterval = 10f; // Time (seconds) between spawns
|
||||
public int maxCartsInScene = 5; // Prevent too many carts
|
||||
|
||||
private int currentCarts = 0;
|
||||
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(SpawnCartsOverTime());
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator SpawnCartsOverTime()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (currentCarts < maxCartsInScene)
|
||||
{
|
||||
Vector3 spawnPosition = GetRandomPositionInArea();
|
||||
GameObject newCart = Instantiate(cartPrefab, spawnPosition, Quaternion.identity);
|
||||
currentCarts++;
|
||||
|
||||
// When cart is returned/destroyed, decrement the counter
|
||||
CartTracker tracker = newCart.AddComponent<CartTracker>();
|
||||
tracker.spawner = this;
|
||||
}
|
||||
|
||||
yield return new WaitForSeconds(spawnInterval);
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 GetRandomPositionInArea()
|
||||
{
|
||||
float x = Random.Range(-spawnAreaSize.x / 2, spawnAreaSize.x / 2);
|
||||
float z = Random.Range(-spawnAreaSize.z / 2, spawnAreaSize.z / 2);
|
||||
float y = spawnAreaCenter.y;
|
||||
|
||||
return spawnAreaCenter + new Vector3(x, 0f, z);
|
||||
}
|
||||
|
||||
public void CartRemoved()
|
||||
{
|
||||
currentCarts--;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue