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 genfu7 · Sep 06, 2012 at 08:57 PM · prefablevellooping

How to make looping/generative level segments

I'm trying to make a side-scrolling 2.5d game in which sections of the levels are prefabs, and are triggered just before the player reaches them (so they appear offscreen before you get there). So far the crude manner in which i have got something similar to work is by having an invisible collider/trigger object which the player's craft hits, when it hits it the game instantiates a prefab level section ahead of the player.

It sort of works, but it is unreliable: the location of the prefab level segment seems to vary with respect to the x axis (the direction the player craft is moving in), and sometimes multiple level segments get instantiated when only 1 should be expected.

So my question is, how would you approach this in a more efficient manner? The idea is to eventually be able to have the level segments selected at random from a selection of different ones, to create an infinite level which isn't preprogrammed (sort of like templerun). Obviously i ought to be destroying old segments too once they are offscreen.

 // instantiates a new level segment on collision.  doesn't seem to work that well though, so different means to do this is required.
     void OnTriggerEnter(Collider other){
         if(other.gameObject.tag == "Player"){
             Debug.Log("Trigger Next Level Segment");
             Rigidbody newLevelSegment = Instantiate(levelsegment, transform.position = new Vector3((transform.position.x + xOffset),yOffset,zOffset), transform.rotation) as Rigidbody;
                 
         }
     }
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
Best Answer

Answer by SentreStage · Sep 06, 2012 at 09:34 PM

Hey there,

Lets start off by addressing your smaller issues.

  1. Sometimes multiple instances get instantiated of the level segment.

A.) You need some code to prevent the OnTriggerEnter from firing twice.. bool alreadyTriggered = false;

 void OnTriggerEnter(Collider other)
 {
     if(other.gameObject.tag == "Player" && !alreadyTriggered)
     {
         alreadyTriggered = true;
         //Do work here
     }
 }

That is how you prevent a method from running multiple times. If you want it to be able to be triggered multiple times, but not simultaneously, then just set "alreadyTriggered = false;" at the end of the method. This will prevent it from firing if its already running, but when it finishes, it will be able to fire again.

  1. The instantiated location seems to vary

A.) The best method for random level generation is to pick one of the following two options:

Option 1: Set up a grid or path , using empty gameObjects as nodes, and instantiate your next prefab section at the next node in the list. The catch to this is that (if horizontally) you need to make sure all your level segments are the same width.

Option 2: You detect the extents of previous segment, compare it to the extents of the segment you are about to Instantiate, and you get an offset point at which to spawn your level segment. Keep in mind, most object instantiation points, are based on the center of the object. (because in Unity, this is where the pivot point is usually at).

Comment
Add comment · Show 2 · 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 genfu7 · Sep 07, 2012 at 09:22 AM 0
Share

yes, that is extremely helpful, thankyou very much!

avatar image SentreStage · Sep 07, 2012 at 01:13 PM 0
Share

Not a problem at all, just trying to help :)

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

8 People are following this question.

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

Related Questions

Scrolling level Instantiate after cycle 0 Answers

Saving Space with Prefabs 0 Answers

Why my prefab is destroyed after reloading level? 2 Answers

Looping a 2d level 0 Answers

How to load enemies according to enemy list? 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