Im trying to figure out a way of randomly generating a 2d level for a side scroller game. I have a flat level floor all ready to go but I’m currently working on platforms for the player to jump on to. I can get the platforms to spawn, but i would like it to be more random. Any help would be greatly appreciated.
var blockplatform:GameObject;
var blockplatform2:GameObject;
var blockplatform3:GameObject;
var lastplatform:GameObject;
var lastplatform2:GameObject;
var lastplatform3:GameObject;
private var deltaDistance:float;
private var nextStop:Vector3;
private var nextStop2:Vector3;
private var nextStop3:Vector3;
function Start(){
nextStop = transform.position + Vector3.forward * 0.1;
nextStop2 = transform.position + Vector3.forward * 0.1;
nextStop3 = transform.position + Vector3.forward * 0.1;
}
function Update () {
var currPos:Vector3 =transform.position;
var currPos2:Vector3 =transform.position;
var currPos3:Vector3 =transform.position;
//print(Vector3.Distance(nextStop3,currPos3));
if(Vector3.Distance(nextStop,currPos)>20){
CreateNewplatform();
nextStop = transform.position + Vector3.forward * 0.1;
}
if(Vector3.Distance(nextStop2,currPos2)>80){
CreateNewplatform2();
nextStop2 = transform.position + Vector3.forward * 0.1;
}
if(Vector3.Distance(nextStop3,currPos3)>100){
CreateNewplatform3();
nextStop3 = transform.position + Vector3.forward * 0.1;
}
}
function CreateNewplatform(){
var insPos:Vector3 = lastplatform.transform.Find("end_end").transform.position;
var newplatform:GameObject = Instantiate(blockplatform,insPos, lastplatform.transform.rotation);
lastplatform = newplatform;
}
function CreateNewplatform2(){
var insPos:Vector3 = lastplatform2.transform.Find("end_end2").transform.position;
var newplatform2:GameObject = Instantiate(blockplatform2,insPos, lastplatform2.transform.rotation);
lastplatform2 = newplatform2;
}
function CreateNewplatform3(){
var insPos:Vector3 = lastplatform3.transform.Find("end_end3").transform.position;
var newplatform3:GameObject = Instantiate(blockplatform3,insPos, lastplatform3.transform.rotation);
lastplatform3 = newplatform3;
}