I have to make for school a simple temple run game and I just need some help with coding. I need ideas how to generate a terrain that goes straight forward, right, left and with T-split.
Here is some code I made so far:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class World : MonoBehaviour {
enum Directions {
Straight = 0,
Left = 1,
Right = 2,
Tsplit = 3
}
public Transform prefab, prefab2;
public static float pre1;
public int numberOfObjects;
private Vector3 nextPosition;
private float recycleOffset;
private Queue<Transform> objectQueue;
private int direction;
private float numberObjects = 0;
private bool switchDirection = true;
void Start () {
recycleOffset = prefab.localScale.x + prefab.localScale.z;
direction = 0; // onthoud dit
nextPosition = transform.localPosition;
}
void Update () {
pre1 = nextPosition.x;
Debug.Log(pre1);
if(nextPosition.x - recycleOffset < Player.distanceTraveled.x){
switch(direction)
{
case (int)Directions.Straight:
Straight();
break;
case (int)Directions.Left:
Left();
break;
case (int)Directions.Right:
Right();
break;
case (int)Directions.Tsplit:
// T-splitsing
break;
}
}
}
private void Straight () { // Dit is 0
Debug.Log(direction + "Straight");
numberObjects += 1;
Vector3 position = nextPosition;
for (int i = 0; i < numberOfObjects; i++) {
Instantiate(prefab, nextPosition, prefab.rotation);
nextPosition += new Vector3(
prefab.localScale.x, 0,0);
if(numberObjects == i && switchDirection) {
switchDirection = false;
numberObjects = 0;
direction = Random.Range(1, 3);
}
}
Instantiate(prefab2, nextPosition, prefab.rotation);
}
private void Left () { // Dit is 1
Debug.Log(direction + "Left");
numberObjects += 1;
Vector3 position = nextPosition;
for (int i = 0; i < numberOfObjects; i++) {
Instantiate(prefab, nextPosition, prefab.rotation);
nextPosition += new Vector3(
0, 0, -prefab.localScale.x);
if(numberObjects == i && !switchDirection) {
switchDirection = false;
numberObjects = 0;
direction = 0;
if (direction == 0) {
switchDirection = true;
}
}
}
Instantiate(prefab2, nextPosition, prefab.rotation);
}
private void Right () { // Dit is 2
Debug.Log(direction + "Right");
numberObjects += 1;
Vector3 position = nextPosition;
for (int i = 0; i < numberOfObjects; i++) {
Instantiate(prefab, nextPosition, prefab.rotation);
nextPosition += new Vector3(
0, 0, prefab.localScale.x);
if(numberObjects == i && !switchDirection) {
switchDirection = false;
numberObjects = 0;
direction = 0;
if (direction == 0) {
switchDirection = true;
}
}
}
Instantiate(prefab2, nextPosition, prefab.rotation);
}
}
This piece of code is to generate a terrain based on the x value of the player.
When the x value is greater then the nextPostion (this is written inside the script World) then it randomly generates a next terrain, this can be right, left or T-split.
Right now I got code to generate a terrain to left, right and straight forward.
But how can I implement a T-split in code and when a T-split is generated that it builds further on which direction the player choosed.
It sounds all complex but I think is it all quiet simple but I can’t come with it right so I really need some help for this school project.
Any code or tips are helpfull,
Thanks in advance.