Space-Smash-Out/Assets/Scripts/TargetSpawner.cs

50 lines
1.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetSpawner : MonoBehaviour
{
public int xRange = 30;
public int yRange = 30;
public GameObject targetPrefab;
private List<GameObject> targetList = new();
// Start is called before the first frame update
void Start()
{
SpawnTarget();
}
private void SpawnTarget(bool spawnRandom = true, Vector3 spawnPosition = new Vector3())
{
int xPos = (int)spawnPosition.x;
int yPos = (int)spawnPosition.y;
if (spawnRandom)
{
xPos = Random.Range(-xRange, xRange);
yPos = Random.Range(-yRange, yRange);
}
var target = Instantiate(targetPrefab);
target.transform.SetParent(transform);
targetList.Add(target);
target.transform.localPosition = new Vector3(xPos, yPos, 0);
target.GetComponent<HitFeedbackTestObjectBehavior>().spawner =
gameObject.GetComponent<TargetSpawner>();
}
void Update()
{
if (targetList.Count < 1)
{
SpawnTarget();
}
}
public void TargetDestroyed(GameObject go)
{
targetList.Remove(go);
}
}