76 lines
1.8 KiB
C#
76 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
public class CakeSpawner : MonoBehaviour
|
|
{
|
|
public GameObject cakePrefab;
|
|
|
|
public SpriteRenderer gameBoardSpriteRenderer;
|
|
|
|
private Bounds cakeBounds;
|
|
|
|
private Bounds gameBoardBounds;
|
|
|
|
private float spawnTimer = 0f;
|
|
|
|
private float spawnInterval = 10f;
|
|
|
|
private Vector2 spawnPosition;
|
|
|
|
private bool spawned = false;
|
|
|
|
private static float spawnProbability = 0.9f;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
gameBoardBounds = gameBoardSpriteRenderer.bounds;
|
|
cakeBounds = cakePrefab.GetComponent<SpriteRenderer>().bounds;
|
|
Reset();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
if (!spawned)
|
|
{
|
|
spawnTimer += Time.fixedDeltaTime;
|
|
|
|
if (spawnTimer >= 1f)
|
|
{
|
|
spawnTimer = 0f;
|
|
if (Random.value < spawnProbability)
|
|
{
|
|
GameObject cake = Instantiate(cakePrefab, spawnPosition, Quaternion.identity);
|
|
}
|
|
|
|
spawned = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
spawnTimer += Time.fixedDeltaTime;
|
|
|
|
if (spawnTimer >= spawnInterval)
|
|
{
|
|
Reset();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
spawned = false;
|
|
spawnTimer = 0f;
|
|
spawnPosition = new Vector2(
|
|
Random.Range(
|
|
gameBoardBounds.min.x + cakeBounds.extents.x,
|
|
gameBoardBounds.max.x - cakeBounds.extents.x
|
|
),
|
|
Random.Range(
|
|
gameBoardBounds.min.y + cakeBounds.extents.y,
|
|
gameBoardBounds.max.y - cakeBounds.extents.y
|
|
)
|
|
);
|
|
}
|
|
}
|