x


Procedural Top Down Levels

I'm building a top down game wherein I wish the environment around the player to be generated randomly. Essentially I wish objects to be spawned out of the view in a radius around the player and the be destroyed when they are a certain distance away. At this stage it can just be a single object.

Before I attempted to build this myself I wanted to ask a few more experienced Unity users how they would tackle this question?

I was thinking of perhaps using a Bounds where the area between the extents and size was the spawnable area, however I'm at a loss on how I would calculate points only in that area.

Thanks, any help is much appreciated, ~Hawk

more ▼

asked Jan 21 '10 at 04:13 AM

Hawkules gravatar image

Hawkules
3 3 3 4

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

1 answer: sort voted first

You can use something like this:

var p : Vector3 = Random.insideUnitSphere * maxSpawnRange;
p.y = 0;
if (p.sqrMagnitude >= minSpawnRange*minSpawnRange) {
    p += player.transform.position;
    //Spawn the object at position P
}

It will generate a random position but discard it if the position is too close to the player.

You can use the same principle when destroying objects too far away:

if ((transform.position - player.transform.position).sqrMagnitude > destroyRange*destroyRange) {
    Destroy (gameObject);
}

Run this code every second or so.

more ▼

answered Jan 21 '10 at 06:44 AM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

Thanks, that is a quicker way than I was thinking.

Jan 21 '10 at 09:00 AM Hawkules
(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:

x300

asked: Jan 21 '10 at 04:13 AM

Seen: 1138 times

Last Updated: Jan 21 '10 at 04:13 AM