31 lines
1019 B
C#
31 lines
1019 B
C#
using UnityEngine;
|
|
|
|
public class ButterflySpawner : MonoBehaviour
|
|
{
|
|
public GameObject[] butterflyPrefabs; // Assign all 3 butterfly prefabs in the Inspector
|
|
public int spawnDepth = 10; // Distance from camera if needed
|
|
|
|
public void SpawnButterflies(int count)
|
|
{
|
|
Camera cam = Camera.main;
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector3 viewportPos = new Vector3(
|
|
Random.Range(0.3f, 0.7f), // X: inside screen bounds
|
|
Random.Range(0.5f, 0.7f), // Y: avoid too low/high
|
|
spawnDepth
|
|
);
|
|
|
|
Vector3 worldPos = cam.ViewportToWorldPoint(viewportPos);
|
|
worldPos.z = 0f; // Force Z to 0 if you're working in 2D
|
|
|
|
// Randomly pick one of the butterfly prefabs
|
|
int randomIndex = Random.Range(0, butterflyPrefabs.Length);
|
|
GameObject chosenPrefab = butterflyPrefabs[randomIndex];
|
|
|
|
Instantiate(chosenPrefab, worldPos, Quaternion.identity);
|
|
}
|
|
}
|
|
}
|