- Home /
Temple run game
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.
Have you finish this project? maybe you could share the continuation of this code for educational purposes
Answer by Wuzseen · Jun 26, 2013 at 02:58 PM
I think you may want to think about procedural mesh generation.
http://blog.nobel-joergensen.com/2010/12/25/procedural-generated-mesh-in-unity/
That's a great blog post that takes you through how to do it in unity.
http://8bitmemories.blogspot.com/2011/10/procedural-track-generation-in-unity3d.html
That's another good resource that shows how you might generate it along a path. It uses hermite splines but you can use the same concept along linear points instead of trying to interpolate in a more complex manner.
Using prefabs is a fine approach though, the key is usally to instantiate them at the extreme edge of the previous prefab. The trick is coming up with the logic that lays the path. Procedural generation lets you leverage more control over the pathing which is why I would suggest that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104912im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making a 3d endless runner? 1 Answer
Creating A Prefab Loses Hiearchy Connections 2 Answers
Box collider doesnt detect collision 0 Answers