Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by helloiam · Jun 26, 2013 at 02:54 PM · prefabsworldrunnerideas

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jeremybanaag · Oct 19, 2016 at 07:54 AM 0
Share

Have you finish this project? maybe you could share the continuation of this code for educational purposes

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Stacking level blocks in an Infinite runner game 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges