27 lines
807 B
C#
27 lines
807 B
C#
using UnityEngine;
|
|
|
|
public class ButterflySpawner : MonoBehaviour
|
|
{
|
|
public GameObject butterflyPrefab;
|
|
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.1f, 0.9f), // X: inside screen bounds
|
|
Random.Range(0.2f, 0.8f), // Y: avoid too low/high
|
|
spawnDepth // Z: depth from camera
|
|
);
|
|
|
|
Vector3 worldPos = cam.ViewportToWorldPoint(viewportPos);
|
|
worldPos.z = 0f; // Force Z to 0 if you're working in 2D
|
|
|
|
Instantiate(butterflyPrefab, worldPos, Quaternion.identity);
|
|
}
|
|
}
|
|
}
|