Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by bokchoy4221 · Dec 07, 2016 at 08:24 AM · spawnplatformerinstantiate prefabendless runnerplatforms

How to Instantiate platforms with certain space inbetween each platform (Endless Runner)

Hey guys,

So i am creating a platform runner (yes like the thousands out there) and need some help. I am able to get the platforms to spawn and move towards the player. Everything is going well until i figured out the platforms will need to be different lengths.

If the platforms are the same size, they spawn with a space in between each, which is great. But if for example a very long platform spawns, it takes time for it to fully move out of the spawn point. Due to the length of this long platform, another platform will spawn on top of it which is the issue.

I need some help with figuring out how i could code and spawn different length platforms without them overlapping each other and also have a randomized space in between each platform.

Here is my current code. Now i am able to change the frequency of the platforms spawning but then that will cause huge gaps in-between smaller length platforms.

{

 public float scrollSpeed;
 public GameObject[] obstacles;
 public float frequency;
 float counter = 0.0f;
 //spawn point
 public Transform obstacleSpawnPoint;
 // Use this for initialization

 void Start () {
     scrollSpeed = 5;
     frequency = 0.5f;
 }
 
 // Update is called once per frame
 void Update () {

     //instantiate object
     if (counter <= 0.0f)
     {
         SpawnObstacle();
     }
     else
     {
         counter -= Time.deltaTime * frequency;
     }
     GameObject currentChild;


         //this scrolls obstacles
         for (int i = 0; i < transform.childCount; i++)
     {
         currentChild = transform.GetChild(i).gameObject;
         MoveObstacle(currentChild);
         if(currentChild.transform.position.x <= -15.0f)
         {
             Destroy(currentChild);
         }

     }


 }


 void MoveObstacle(GameObject obstacle)
 {
     obstacle.transform.position -= Vector3.right * (scrollSpeed * Time.deltaTime);
 }

 void SpawnObstacle()
 {
     GameObject newObstacle = Instantiate(obstacles[Random.Range(0, obstacles.Length)], obstacleSpawnPoint.position, Quaternion.identity) as GameObject;
     newObstacle.transform.parent = transform;
     counter = 1;

 }

 }

Thanks

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 doublemax · Dec 07, 2016 at 08:36 AM 0
Share

You could store the length of each platform somewhere and make the spawntime dynamically.

Or maybe you can add an empty gameobject to each platform and place it at the end as a kind of "end marker". Then you use only this marker to detect when to spawn a new platform.

BTW: From a performance point of view it's not a good idea to instantiate new objects constantly. Use a object pool. Hide the objects that are not used and re-use them when needed.

$$anonymous$$any of the official Unity samples use object pools. Check it out.

0 Replies

· Add your reply
  • Sort: 

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

80 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 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 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 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

Endless - Slight gaps that I cannot explain 0 Answers

reference instantiated prefab on a scroll list? 0 Answers

Moving left and right with one button in 2d Game 1 Answer

How To Generate Platforms Down Each other in a pattern with some different positions Unity 1 Answer

Endless Runner platform Heigh generator not working. 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