using System.Collections; using System.Collections.Generic; using UnityEngine; public…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BarrelSpawner : MonoBehaviour { public GameObject barrelPrefab; public Transform[] spawnPoints; // Points where barrels will be spawned public float spawnInterval = 3f; // Interval between barrel spawns
private bool isSpawning = true; private void Start() { InvokeRepeating("SpawnBarrel", 0f, spawnInterval); } private void SpawnBarrel() { int spawnIndex = Random.Range(0, spawnPoints.Length); Transform spawnPoint = spawnPoints[spawnIndex]; Instantiate(barrelPrefab, spawnPoint.position, Quaternion.identity); } public void StopSpawning() { isSpawning = false; CancelInvoke("SpawnBarrel"); }}
Add to this script to make it so that upon the start function 4 barrels are spawned in at each of the respective positions: (-6, -3.25, 0), (6, 1.25,0), (-6, 0.75, 0), (6, 2.75, 0)
- Assistant
You can modify the
Startfunction in theBarrelSpawnerscript to spawn 4 barrels at specific positions. Here's the updated code:private void Start() { InvokeRepeating("SpawnBarrel", 0f, spawnInterval); // Spawn initial barrels SpawnInitialBarrels(); } private void SpawnInitialBarrels() { // Create an array of positions Vector3[] initialPositions = new Vector3[] { new Vector3(-6f, -3.25f, 0f), new Vector3(6f, 1.25f, 0f), new Vector3(-6f, 0.75f, 0f), new Vector3(6f, 2.75f, 0f) }; // Loop through each position and spawn a barrel foreach (Vector3 position in initialPositions) { Instantiate(barrelPrefab, position, Quaternion.identity); } }By adding the
SpawnInitialBarrelsmethod to theStartfunction, it will be called when the game starts, and it will instantiate thebarrelPrefabat each of the specified positions.