- Home /
2D Platform Generation: Spawning Platforms Consecutively
Hey there fellow Unity devs;
I have been playing around with the following script for a few hours now trying to get a platform to spawn once at a fixed location and then spawn another platform at the edge of the previous platform after a delay.
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;
// Use this for initialization
void Start () {
Spawn();
}
void Spawn()
{
Instantiate (obj [Random.Range (0, obj.GetLength (0))], transform.position, Quaternion.identity);
Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
}
}
I have tried a few different methods that I have found around the answers listings but haven't gotten it working yet.
I think the problem has something to do with how I am instructing the game to spawn the next platform. The next platform should spawn at the edge of the previously spawned platform, however I think that the fact that this spawn block is following the camera is messing it up.
How can I compensate for the camera movement or how can I do this without attaching the script to a block following the camera?
Thanks for any help.
Answer by fifthknotch · Mar 06, 2014 at 04:18 AM
I don't know exactly how you want your game to look and feel, but I would work it like this:
Load your level with one platform (can be random of course). Attach to your platform the script saying to instantiate the next platform, but do so only if the platform is visible (void OnBecameVisible - builtin unity function). Here's why :
You will start with a scene that has one random platform instantiate. Then it will instantiate the next because it is visible, which will instantiate the next platform, and so on, until a platform leaves the view of your camera. Then as your player moves, that last platform will come into sight and instantiate a platform next it, just out of view. This will result in an infinite amount of random platforms being spawned as your player moves in one direction. If you want any type of delay, yield statements work nicely.
Answer by robertbu · Mar 06, 2014 at 04:42 AM
You have some definition issues here. First you say "at the edge of the previously spawned platform." So is that the left edge, right edge, front, back? Second, when placing so that edges touch, you need to know the size of the item. So for a simple example, lets say that the objects are 1 unit in width, and you want to grow them from left to right. Here is a bit of example code. I've changed your chained Invoke() calls to a coroutine. You should initialize your 'obj' with a prefab of a sphere and a cube (both 1 unit in size). The initial value of 'pos' is where the first one will be placed:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;
public Vector3 pos = new Vector3(-4,0,0);
public float size = 1.0f;
private Vector3 dir = Vector3.right;
void Start () {
StartCoroutine(Spawn());
}
IEnumerator Spawn() {
while (true) {
Instantiate (obj [Random.Range (0, obj.Length)], pos, Quaternion.identity);
pos += dir * size;
yield return new WaitForSeconds(Random.Range (spawnMin, spawnMax));
}
}
}
Answer by Kensharma · Mar 06, 2014 at 09:57 PM
I actually solved this myself by modifying the gameobject this script was attached to so that rather then following the camera, it moves at a fixed interval (each movement is the size of the platform) and then modified the script so that it will spawn after each movement.
Does this work just as well or should I opt for another method? All my platforms are the same length so that isn't a problem.
As long as you don't have it spawning platforms in an infinite loop - all is well as far as I can tell.