Scripting help

Hi, I’m currently trying to develop a 2D endless runner; my main trouble has been generating the terrain and making the prefabs stick together… What I want to do is basically have 10 terrain prefabs (one of which is only used once at the beginning) and pick one randomly then spawn it, deleting the previous one.
I’ll always have two prefabs at runtime, to make a nice transition if you know what I mean.

I just turned 18 and my scripting is pretty amateurish, so I’m seeking some help to make it more efficient.

Even if anyone has something to offer in javascript, I’ll take it.

Thank you in advance

using UnityEngine;
using System.Collections;

public class TerrainGenerator : MonoBehaviour {


	GameObject firstPlatform = null;

	//New platform
	GameObject newPlatform = null;

	//transition, platform that is going to be destroyed
	GameObject transPlatform = null;

	//Booleans

	//Check if new platform has arrived
	bool isNew = false;

	//Check if platform has been destroyed
	bool isDestroyed = false;

	bool firstPlatIsThere = true;

	//Platform movement
	public float gameSpeed = -5f;

	float destroyPointX = -25f;

	//Prefabs defined in Inspector
	public GameObject[] randomPrefab = new GameObject[10];


	void EndlessTerrain () {

		GameObject selectedPrefab = randomPrefab[Random.Range(1,9)];

		if (isNew == false  isDestroyed == false) {

			newPlatform = Instantiate(selectedPrefab, new Vector3(25,0,0), Quaternion.identity) as GameObject;

			isNew = true;

			isDestroyed = true;
		}
			newPlatform.transform.Translate(gameSpeed * Time.deltaTime,0,0);


		if (newPlatform.transform.position.x < 0){

				transPlatform = newPlatform;

				newPlatform = Instantiate(selectedPrefab, new Vector3(25,0,0), Quaternion.identity) as GameObject;
			}

		if (transPlatform != null){

			transPlatform.transform.Translate(gameSpeed * Time.deltaTime,0,0);

		if (transPlatform.transform.position.x < destroyPointX){

			GameObject.Destroy(transPlatform);

			}	

		}

	}


	void FirstPlatform () {

		while (firstPlatIsThere == true){
			
			firstPlatform = Instantiate(randomPrefab[0], Vector3.zero,Quaternion.identity) as GameObject;

			firstPlatIsThere = false;

		}

		if (firstPlatform != null) {

		firstPlatform.transform.Translate(gameSpeed * Time.deltaTime,0,0); 

		if (firstPlatform.transform.position.x < destroyPointX){

			GameObject.Destroy(firstPlatform);

			Transform.Destroy(firstPlatform.transform);

				isDestroyed = true;

			}

		}

	}

	void Awake () {
			
	}

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {

		FirstPlatform();

		EndlessTerrain();

		}

}

Unless your terrains are massive, I would just pool them. Keep them all “alive” in scene, just sitting in the background, disabled, so to speak.

I would also make sure, each is identical in size (scale). This will make snapping them into place, relative to each other a snap! Get it?! Haha… but I digress…

Then, I would have a mid way trigger in each. I a point in each prefab that when the player reaches it, it is appropriate to remove the old terrain and load a new one.

So, on player reach mid way trigger…
1 get a random int value
2 use this random int value to apply it to the terrain pool. This pulls out the member of the terrain pool.
3 place this now just pulled member into the appropriate position. It would be something like, current active terrain piece, position + offset.
4 now take the old passed piece and put it back into the pool.

That is the logic I would apply to your system.

Hi thanks for the quick response !

I understand everything you just told me but I don’t really know how pooling works.

Can you explain briefly how I can create this type of system ? And also, should I use a ray for the midway trigger ?

Pooling is a group of a single class of objects. Bullets for instance. The programmer pills a single object from this pool when needed. And replaces it when done. The main reason is that it removes the spike in performance hit caused by instantiation and destruction.

There are plenty of examples posted. I would use a list for your pool.
I will come back later with more help if needed. Just not at the computer, so I can’t show my scripts.

Thank you very much !

I will try out the pooling method and post my code when done for more feedback !

Yes,
Use a list. Store the object in this list as class, terrain.

public List<myTerrainMemeberScript> members;///this could be a transform class too.

Then pull the top one off when needed.

void loadNewTerrain(){
///called via trigger when player hits load new terrain trigger
int rndInt = Random.Range(0,members.Count);
newMember = members[rdmInt];
newMember.position = crntMember.position + offset;
members.RemoveAt(rdmInt);
}


void removeOldTerrain(){
///called when player hits remove old terrain trigger, soon after entering new terrain.
members.Add(crntMember);
crntMember.position = hidePos;
crntMember = newMember;
}

The coding is probably off, but the logic is basically there.

Thank you very much !

Do you know how I can create a trigger on every platform ? Should I use a ray casting ?

EDIT : I found the answer at http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers

Sure thing.
Just ask if you need more help.
Cheers,
Ren

I’m having a hard time setting up a trigger in the middle of the platform, I have no idea what to use.

Can you show me how I can create such a thing please ?

What do you mean? If you can consider you terrain piece a platform, a square platform. I imagine there is a single path, the player moves along. Now, in a position where the player would be, that the camera would no longer see the old platform, and can not see the next platform, or where it would be, place the trigger there,

The trigger, is a gameObject, with a rigidbody and collider attached.