• 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 romeo160705 · Mar 21, 2021 at 10:00 AM · generationendless runnerinfinite runner

Endless Runner world generation stops at certain point.

Hey, I am making a 2d (Verticle) endless runner game, following a tutorial. Now I ran into a problem that my endless level generation stops when I go to high (when i go 3000 higher from start). It doesn't have anything to do with worldorigin, I think it must be in the script somewhere. Here is a video of what happens zoomed out: https://imgur.com/a/7pA13tN And here is the script: private const float PLAYER_DISTANCE_SPAWN_LEVEL_PART = 250f;

     [SerializeField] private List<Transform> levelPartList;
     [SerializeField] private Transform levelPart_Start;
     [SerializeField] private Player player;
 
     private Vector3 lastEndPosition;
     private void Awake()
     {
 
         lastEndPosition = levelPart_Start.Find("EndPosition").position;
 
         int startingSpawnLevelParts = 20;
         for (int i = 0; i < startingSpawnLevelParts; i++)
         {
             SpawnLevelPart();
         }
 
 
     }
 
     private void Update()
     {
         if (Vector3.Distance(player.transform.position, lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART)
         {
             SpawnLevelPart();
         }
     }
 
     private void SpawnLevelPart()
     {
         Transform chosenLevelPart = levelPartList[Random.Range(0, levelPartList.Count)];
         Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
         lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
     }
 
 
     private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition)
     {
         Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
         return levelPartTransform;
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toficofi · Mar 21, 2021 at 12:54 PM

It's quite hard to tell from the video but it looks like the player slowly "catches up" to the new level parts and eventually overtakes them, in which case Vector3.Distance(player.transform.position, lastEndPosition) would start to increase again.

I don't know how your player controller looks, but is it possible you're moving the player in FixedUpdate, in which case it might be running more frequently than your level generation?

Comment
Add comment · Show 4 · 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 romeo160705 · Mar 21, 2021 at 12:58 PM 0
Share

Thanks for your help! I'm currently not at my PC but I wil check it out when I'm back.

avatar image romeo160705 · Mar 21, 2021 at 03:31 PM 0
Share

@toficofi Here is my player script, it doesn't matter if put it in Upate or FixedUpdate.

 void FixedUpdate()
     {
         if (ispressed)
         {
             thruster.Emit(1); //Emit some particle
             RaycastHit2D hit = Physics2D.Raycast(rayOrigin.transform.position, Vector2.down, rayCheckDistance);
                 if (hit.collider)
                 {
                     rb.AddForce(Vector2.up * jump, ForceMode2D.Impulse);
                 }           
         }        
     }
 
     public void OnPointerDown(PointerEventData eventData)
     {
         ispressed = true;
     }
 
     public void OnPointerUp(PointerEventData eventData)
     {
         ispressed = false;
     }
avatar image toficofi romeo160705 · Mar 21, 2021 at 03:40 PM 0
Share

You're using physics to move the player, so that will move in FixedUpdate either way. You could try changing the generator to use FixedUpdate instead, or make the generator create a bunch of level parts at once.

avatar image romeo160705 toficofi · Mar 21, 2021 at 03:59 PM 0
Share

Okay, I will try that

avatar image
0

Answer by rh_galaxy · Mar 21, 2021 at 04:04 PM

If you need to spawn more than one levelPart per frame your code will not work.

Try

 private void Update()
 {
     while(Vector3.Distance(player.transform.position, lastEndPosition)<PLAYER_DISTANCE_SPAWN_LEVEL_PART)
     {
         SpawnLevelPart();
     }
 }

But Vector3.Distance is maybe also not correct to do, since it will measure the the distance regardless of the player is in front of the level part or behind it, that will cause you to stop adding level parts when too much in front like what is seen in your clip.

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 romeo160705 · Mar 21, 2021 at 04:20 PM 0
Share

@rh_galaxy Thanks for your help! First I could go max at y = 3000 Now it is still not completely infinite I can go around y = 5700, but I don't think anyone can reach that. I cant come up with an explanation why it stops there, really weird, or I'm just stupid...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

2d Endless Runner: Dynamically generating a multi-lane path 0 Answers

4 C# errors Endless Runner 0 Answers

Random obstacles spawn in 2D side endless runner game ? 2 Answers

How can i make the game check if a certain input is executed in a certain time frame? 1 Answer

Platform Generation For Endless Runner 1 Answer


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