- Home /
Keep random Instantiation from spawning ontop of other objects
Hey everyone anyone know how to keep randomly instantiated objects from spawning on top of other objects like walls or previously spawned objects.
here's the random spawn script:
var floorHeight = -0.8f;// y axiss var xAxis; var zAxis; var mine : GameObject; static var spawn = 0; var target : MoveToGround;
function Update () {
while (spawn <= 10) { var xAxis = Random.Range(-18,18); var zAxis = Random.Range(-17,21); //print(zAxis); Instantiate(mine, Vector3(xAxis, floorHeight,zAxis), Quaternion.identity); target.toGround = true; spawn++; }
}
Answer by Lab013 · Mar 17, 2011 at 02:04 AM
Put an object that detects collisions within the spawn range, upon collision it deactivates that spawn or displaces the spawn to a new location.
So basically oncollision with the off limits part delete the object and subtract 1 from spawn?(spawn is the number of objects tht will b instantiated)
Depending on your set up, that would be a solution. Of course there are many implementations to many solutions though.
Yup works great. Thank you very much. Now would I be able to do the same to keep objects a certain distance away. Like adding a sphere collider around the object and if another object is inside it deletes and adds back to spawn? is there an easier way?
You could use a sphere collider, or calculate the distance in-between objects. I recommend you also ray cast (Physics.Raycast) to make sure there isn't a wall between one player/enemy and the spawn, because if there is a wall then it doesn't matter if they're close.