- Home /
Endless Runner with List
Hi! I have written a code to generate endless platforms (always the same) and works perfectly.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlatformGenerator : MonoBehaviour {
//Check position character player and regenerates platforms
public GameObject playerRef;
//Unit distance to start recycle prefabs
public float DistanceToRecycle = 25;
//Set up maximum number of platform to draw
public int NumberOfPlatforms = 5;
//Set up start point and next position
public Vector3 startPosition;
private Vector3 nextPosition;
//Gameobject empty startpoint to generate platform
public Transform prefab;
public List<GameObject> platforms = new List<GameObject>();
//Settings dimension, distance and position platforms
public Vector3 minScale = new Vector3();
public Vector3 maxScale = new Vector3();
public float minGap;
public float maxGap;
public Vector3 minHeight = new Vector3();
public Vector3 maxHeight = new Vector3();
// Use this for initialization
void Start () {
//Find target
playerRef = GameObject.FindGameObjectWithTag("Player");
nextPosition = startPosition;
for (int i = 0; i < NumberOfPlatforms; i++) {
Transform startPoint = (Transform)Instantiate(prefab);
startPoint.localPosition = nextPosition;
nextPosition.x += startPoint.localScale.x;
}
//Loading platforms prefab and draws maximum number of platforms from folder"Resources/Prefabs/Platforms/Platform1"
GameObject platformPrefab = Resources.Load ("Prefabs/Platforms/Platform1") as GameObject;
for (int i=0; i<NumberOfPlatforms; i++){
GameObject floor = Instantiate (platformPrefab,Vector3.zero,Quaternion.identity) as GameObject;
//Generate a random scale dimension of platform prefab
Vector3 tempScale = new Vector3(Random.Range (minScale.x,maxScale.x),1,1);
floor.transform.localScale = tempScale;
//Generate platform prefab adjacent sequence
Vector3 tmpPos = new Vector3();
if(platforms.Count>0) {
tmpPos = platforms[platforms.Count-1].transform.position;
tmpPos.x += platforms[platforms.Count-1].transform.localScale.x *.5f;
tmpPos.y = Random.Range(minHeight.y, maxHeight.y);
tmpPos.x += floor.transform.localScale.x *.5f;
}
tmpPos.x += Random.Range(minGap, maxGap);
floor.transform.position = tmpPos;
floor.transform.parent = gameObject.transform;
platforms.Add(floor);
}
}
// Update is called once per frame
void Update() {
//Check distance between player and last platform and remove old for generate new platforms
if(platforms[0].transform.position.x + DistanceToRecycle < playerRef.transform.position.x){
GameObject floor = platforms[0];
platforms.Remove (floor);
//Generate a random scale dimension of platform prefab
Vector3 tmpScale = new Vector3(Random.Range (minScale.x,maxScale.x),1,1);
floor.transform.localScale = tmpScale;
//Generate platform prefab adjacent sequence
Vector3 tmpPos = new Vector3();
if(platforms.Count>0) {
tmpPos = platforms[platforms.Count-1].transform.position;
tmpPos.x += platforms[platforms.Count-1].transform.localScale.x *.5f;
tmpPos.y = Random.Range(minHeight.y, maxHeight.y);
tmpPos.x += floor.transform.localScale.x *.5f;
}
//Generate random distance between platforms
tmpPos.x += Random.Range(minGap, maxGap);
floor.transform.position = tmpPos;
floor.transform.parent = gameObject.transform;
platforms.Add (floor);
}
}
}
I started with this code base to create one that would generate different platforms from a prefabs list with the command LIST. BUT I have the problem that the prefabs are generated as distant as you see in the image.
Now the revised script to generate X prefabs from a predefined list of different prefabs. using UnityEngine; using System.Collections; using System.Collections.Generic;
public class PlatformGenerator : MonoBehaviour {
//Check position character player and regenerates platforms
public GameObject playerRef;
//Gameobject empty to create start point to generate platform
public Transform prefab;
//Set up maximum number of platform to draw
public int numberOfPlatforms;
//Set parameter units distance to regenerate platforms
public float DistanceToRecycle;
//Set up start point and next position
public Vector3 startPosition;
private Vector3 nextPosition;
//My Prefabs List's
public List<GameObject> platforms;
public float minGap, maxGap;
public Vector3 minHeight = new Vector3();
public Vector3 maxHeight = new Vector3();
// Use this for initialization
void Start () {
//Find target
playerRef = GameObject.FindGameObjectWithTag("Player");
nextPosition = startPosition;
for (int i = 0; i < numberOfPlatforms; i++) {
Transform startPoint = (Transform)Instantiate(prefab);
startPoint.localPosition = nextPosition;
nextPosition.x += startPoint.localScale.x;
}
//Loading platforms prefab and draws maximum number of platforms from "prefab list"
for (int i=0; i<numberOfPlatforms; i++){
GameObject floor = Instantiate(platforms[Random.Range(0,platforms.Count)],Vector3.zero,Quaternion.identity) as GameObject;
//Generate prefabs platforms at different heights
Vector3 tmpPos = new Vector3();
if(platforms.Count>0) {
tmpPos = platforms[platforms.Count-1].transform.position;
tmpPos.x += platforms[platforms.Count-1].transform.localScale.x *.5f;
tmpPos.y = Random.Range(minHeight.y, maxHeight.y);
tmpPos.x += floor.transform.localScale.x *.5f;
}
//Generate random distance between platforms
tmpPos.x += Random.Range(minGap, maxGap);
floor.transform.position = tmpPos;
floor.transform.parent = gameObject.transform;
platforms.Add(floor);
}
}
// Update is called once per frame
void Update() {
//Check distance between player and last platform and remove old for generate new platforms
if(platforms[0].transform.position.x + DistanceToRecycle < playerRef.transform.position.x){
GameObject floor = platforms[0];
platforms.Remove (floor);
//Generate prefabs platforms at different heights
Vector3 tmpPos = new Vector3();
if(platforms.Count>0) {
tmpPos = platforms[platforms.Count-1].transform.position;
tmpPos.x += platforms[platforms.Count-1].transform.localScale.x *.5f;
tmpPos.y = Random.Range(minHeight.y, maxHeight.y);
tmpPos.x += floor.transform.localScale.x *.5f;
}
//Generate random distance between platforms
tmpPos.x += Random.Range(minGap, maxGap);
floor.transform.position = tmpPos;
floor.transform.parent = gameObject.transform;
platforms.Add (floor);
}
}
}
Why with the script that generates the same prefab these are generated just below the player while the script that generates several cabins taken from a list, creates platforms away from the character? Thank you for your help!
Answer by haim96 · Mar 07, 2014 at 11:18 AM
i didn't read all your script but you should check where is the prefab local axis is located. make sure it's not away from the prefab. also, if you imported the platform models from 3d software make sure that the scale of it is 1.
platforms are simple cubes created in unity and the start point is fixed for all items in the game in coordinates 0,0,0.
Thanks for your comment :)
Your answer
Follow this Question
Related Questions
Moving the world or the Player?? 1 Answer
Making a 3d endless runner? 1 Answer
Move camera or terrain in endless runner? 1 Answer
Export objects to a .3DS file at runtime 1 Answer
Endless runner ground movement 0 Answers