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 /
avatar image
0
Question by iOddgamers · Aug 02, 2016 at 12:05 PM · c#unity 5infiniteinfinite terrain

Hi, I have a problem with my infinite world generation script

Hi, I have a problem with my infinite world generation script, it just creates about 4 more worlds out of the prefab i made and then stops. take a look at my script below

using UnityEngine; using System.Collections.Generic;

public class GeneratorScript : MonoBehaviour { public GameObject[] availableRooms; public List currentRooms; private float screenWidthInPoints;

 public GameObject[] availableObjects;    
 public List<GameObject> objects;

 public float objectsMinDistance = 5.0f;    
 public float objectsMaxDistance = 10.0f;

 public float objectsMinY = -1.4f;
 public float objectsMaxY = 1.4f;

 public float objectsMinRotation = -45.0f;
 public float objectsMaxRotation = 45.0f;


 // Use this for initialization
 void Start () {
     float height = 2.0f * Camera.main.orthographicSize;
     screenWidthInPoints = height * Camera.main.aspect;
 }

 void AddRoom(float farhtestRoomEndX)
 {
     //1
     int randomRoomIndex = Random.Range(0, availableRooms.Length);

     //2
     GameObject room = (GameObject)Instantiate(availableRooms[randomRoomIndex]);

     //3
     float roomWidth = room.transform.FindChild("floor").localScale.x;

     //4
     float roomCenter = farhtestRoomEndX + roomWidth * 0.5f;

     //5
     room.transform.position = new Vector3(roomCenter, 0, 0);

     //6
     currentRooms.Add(room);         
 }

 void GenerateRoomIfRequired()
 {
     //1
     List<GameObject> roomsToRemove = new List<GameObject>();

     //2
     bool addRooms = true;        

     //3
     float playerX = transform.position.x;

     //4
     float removeRoomX = playerX - screenWidthInPoints;        

     //5
     float addRoomX = playerX + screenWidthInPoints;

     //6
     float farthestRoomEndX = 0;

     foreach(var room in currentRooms)
     {
         //7
         float roomWidth = room.transform.FindChild("floor").localScale.x;
         float roomStartX = room.transform.position.x - (roomWidth * 0.5f);    
         float roomEndX = roomStartX + roomWidth;                            

         //8
         if (roomStartX > addRoomX)
             addRooms = false;

         //9
         if (roomEndX < removeRoomX)
             roomsToRemove.Add(room);

         //10
         farthestRoomEndX = Mathf.Max(farthestRoomEndX, roomEndX);
     }

     //11
     foreach(var room in roomsToRemove)
     {
         currentRooms.Remove(room);
         Destroy(room);            
     }

     //12
     if (addRooms)
         AddRoom(farthestRoomEndX);
 }

 void FixedUpdate () 
 {    
     GenerateObjectsIfRequired();
     GenerateRoomIfRequired();
 }

 void GenerateObjectsIfRequired()
 {
     //1
     float playerX = transform.position.x;        
     float removeObjectsX = playerX - screenWidthInPoints;
     float addObjectX = playerX + screenWidthInPoints;
     float farthestObjectX = 0;

     //2
     List<GameObject> objectsToRemove = new List<GameObject>();

     foreach (var obj in objects)
     {
         //3
         float objX = obj.transform.position.x;

         //4
         farthestObjectX = Mathf.Max(farthestObjectX, objX);

         //5
         if (objX < removeObjectsX)            
             objectsToRemove.Add(obj);
     }

     //6
     foreach (var obj in objectsToRemove)
     {
         objects.Remove(obj);
         Destroy(obj);
     }

     //7
     if (farthestObjectX < addObjectX)
         AddObject(farthestObjectX);
 }

 void AddObject(float lastObjectX)
 {
     //1
     int randomIndex = Random.Range(0, availableObjects.Length);

     //2
     GameObject obj = (GameObject)Instantiate(availableObjects[randomIndex]);

     //3
     float objectPositionX = lastObjectX + Random.Range(objectsMinDistance, objectsMaxDistance);
     float randomY = Random.Range(objectsMinY, objectsMaxY);
     obj.transform.position = new Vector3(objectPositionX,randomY,0); 

     //4
     float rotation = Random.Range(objectsMinRotation, objectsMaxRotation);
     obj.transform.rotation = Quaternion.Euler(Vector3.forward * rotation);

     //5
     objects.Add(obj);            
 }

}

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make a game with lots of objects?!! 1 Answer

How to debug infinite loop in Visual Studio? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Only Allow One Click on GameObject? 2 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