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 elelanfri · Dec 01, 2021 at 02:50 PM · gamedeleterunnercancel

Delete sections behind player

Hi! I've developed my 3D "endless runner" game but I can't manage to delete the sections that remains behind the player while he is running in the game.

Here's my code for randomly generate a section every 2 seconds: using System.Collections; using System.Collections.Generic; using UnityEngine;

  public class GenerateLevel : MonoBehaviour
  {
      public GameObject[] section;
      public int zPos = 50;
      public bool creatingSection = false;
      public int secNum;
      
      void Update()
      {
          if (creatingSection == false)
          {
              creatingSection = true;
              StartCoroutine(GenerateSection());
          }
          
      }
  
      IEnumerator GenerateSection()
      {
          secNum=Random.Range(0,3);
          Instantiate(section[secNum], new Vector3(0,0,zPos), Quaternion.identity);
          zPos+=50;
          yield return new WaitForSeconds(2);
          creatingSection=false;
      }
  }

Thank you!

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
0

Answer by MarekRimal · Dec 01, 2021 at 05:41 PM

Hi, you need to somehow store the sections and compare them with the player position. Or I would prefer to make a separate class for Sections and in update check their positions against the player position. I have made a quick script for you. I didnt run it but the idea is like this.

 public class GenerateLevel : MonoBehaviour
 {
     public Player player; // Fill this in inspector with the player instance
 
     public Section[] sectionPrefabs;   // Here I would put all the section prefabs
     public int zPos = 50;
     // public bool creatingSection = false; // No need
     public int secNum;
     
     bool isLevelGenerationActive = true;    // Make this variable false when you would like to stop the generation
 
     void Start()
     {
         StartCoroutine(GenerateSection());
     }
 
     IEnumerator GenerateSection()
     {
       // Use while here since then you dont need to use Update with the creatingSection bool
       while(isLevelGenerationActive)
       {
           secNum=Random.Range(0,3);
           Section sectionInstance = Instantiate(sectionPrefabs[secNum], new Vector3(0,0,zPos), Quaternion.identity);
           sectionInstance.player = player;  
           zPos+=50;
 
           yield return new WaitForSeconds(2);
           // creatingSection=false; // No need
       }
     }
 }
 
 public class Section : MonoBehaviour
 {
     public Player player;
     public float destroyDistanceBehindPlayer;
 
     void Update()
     {   
         if(player.transform.position.z > transform.position.z + destroyDistanceBehindPlayer)
         {
             Destroy(gameObject);
         }
     }
 }

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

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

Making a randomizer script for a basic "Runner" game... 1 Answer

Why i dont have game and scene 1 Answer

Unity deleted my game 2 Answers

New/Load(Continue) in menu 1 Answer

getting udp package info inside unity (GlovePIE) 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