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 platforms = new List();
//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; i0) {
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.
![alt text][1]
[1]: /storage/temp/23215-cend.jpg
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 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; i0) {
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!
↧