Hello,
I’m looking for some help on my ‘upgrade’ spawning.
Requirements:
- Random Location
- Inside Camera View
- Not Colliding with other objects when spawning
- Extra: Later tweaking it that when the camera is moving up it doesn’t spawn to far down etc.
What I have now:
Vector3 spawnPos = Camera.main.ScreenToWorldPoint(new Vector3(UnityEngine.Random.Range(0, Screen.width), UnityEngine.Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
float radius = 5f;
while (Physics.CheckSphere(spawnPos, radius))
spawnPos = Camera.main.ScreenToWorldPoint(new Vector3(UnityEngine.Random.Range(0, Screen.width), UnityEngine.Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
clone_upgrade = Instantiate(_upgrade, spawnPos, Quaternion.identity) as Transform;
soundEffect("SpawnPickup");
Here’s what my demo level looks like ingame:
These are the colliders: (Didn’t clean them up yet, just for testing)
I tried making the radius bigger but I’m not getting a good effect. The objects still spawn inside the gray areas.
Any help would be greatly appreciated.
Just noticed, I’m not using the Physics2D… So I changed my code:
Vector3 spawnPos = Camera.main.ScreenToWorldPoint(new Vector3(UnityEngine.Random.Range(0, Screen.width), UnityEngine.Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
float radius = 5f;
while (Physics2D.OverlapCircle(spawnPos, radius))
spawnPos = Camera.main.ScreenToWorldPoint(new Vector3(UnityEngine.Random.Range(0, Screen.width), UnityEngine.Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
clone_upgrade = Instantiate(_upgrade, spawnPos, Quaternion.identity) as Transform;
soundEffect("SpawnPickup");
Transform _upgradeSpawnParticleClone = Instantiate(upgradeSpawnPrefab, spawnPos, Quaternion.identity) as Transform;
Destroy(_upgradeSpawnParticleClone.gameObject, 2f);
The results are allot better, but still from time to time some of my pickups spawn outside the area I wish. As you can see at the bottom.
Kind regards,
Xercium