x


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++;
}   
}
more ▼

asked Mar 17 '11 at 01:29 AM

G54 gravatar image

G54
30 6 6 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

Put an object that detects collisions within the spawn range, upon collision it deactivates that spawn or displaces the spawn to a new location.

more ▼

answered Mar 17 '11 at 02:04 AM

Lab013 gravatar image

Lab013
385 1 2 10

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)

Mar 17 '11 at 02:21 AM G54

Depending on your set up, that would be a solution. Of course there are many implementations to many solutions though.

Mar 18 '11 at 06:58 AM Lab013

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?

Mar 18 '11 at 07:13 PM G54

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.

Mar 18 '11 at 09:24 PM Lab013
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2087
x1675
x1529
x573

asked: Mar 17 '11 at 01:29 AM

Seen: 1106 times

Last Updated: Mar 17 '11 at 01:29 AM