2022-10-24 22:06:38 -04:00

56 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjSpawner : MonoBehaviour
{
[SerializeField]
private GameObject objPrefab;
[SerializeField]
private float spawnRate;
[SerializeField]
private int amount;
[SerializeField]
private List<GameObject> pool;
private int index = 0;
private int firstIndex = 0;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < amount; i++)
{
GameObject obj = Instantiate(objPrefab, this.transform);
obj.SetActive(false);
pool.Add(obj);
}
//TODO add trigger start
InvokeRepeating("Spawn",0, spawnRate);
}
private void Spawn(){
if(index < amount){
pool[index].transform.position = transform.position;
pool[index].transform.rotation = transform.rotation;
Rigidbody rb = pool[index].GetComponent<Rigidbody>();
if(rb != null){
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
pool[index].SetActive(true);
index++;
}else{
pool[firstIndex].SetActive(false);
index = firstIndex;
firstIndex = firstIndex+1%pool.Count-1;
Spawn();
}
}
// Update is called once per frame
void Update()
{
}
}