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 Greegan · Mar 07, 2014 at 11:15 AM · runtimerunnerendless

Endless Runner with List

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<GameObject> platforms = new List<GameObject>();
 
     //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; i<NumberOfPlatforms; i++){
             GameObject floor = Instantiate (platformPrefab,Vector3.zero,Quaternion.identity) as GameObject;
 
             //Generate a random scale dimension of platform prefab
             Vector3 tempScale = new Vector3(Random.Range (minScale.x,maxScale.x),1,1);
             floor.transform.localScale = tempScale;
 
             //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;
             }
             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

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<GameObject> 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; i<numberOfPlatforms; i++){
             GameObject floor = Instantiate(platforms[Random.Range(0,platforms.Count)],Vector3.zero,Quaternion.identity) as GameObject;
             
             //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);
         }
     }
     // 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!

cend.jpg (36.6 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by haim96 · Mar 07, 2014 at 11:18 AM

i didn't read all your script but you should check where is the prefab local axis is located. make sure it's not away from the prefab. also, if you imported the platform models from 3d software make sure that the scale of it is 1.

Comment
Add comment · Show 1 · 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
avatar image Greegan · Mar 07, 2014 at 11:30 AM 0
Share

platforms are simple cubes created in unity and the start point is fixed for all items in the game in coordinates 0,0,0.

Thanks for your comment :)

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

21 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 avatar image avatar image

Related Questions

Moving the world or the Player?? 1 Answer

Making a 3d endless runner? 1 Answer

Move camera or terrain in endless runner? 1 Answer

Export objects to a .3DS file at runtime 1 Answer

Endless runner ground movement 0 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