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 Mystique5 · Dec 06, 2016 at 02:54 PM · instantiateprocedural generationplanetspaceship

Please Explain the Code

Hello,

So recently I have been trying to make a space based game with procedural generation. I found this piece of code which allows me to procedurally generate planets, asteroids and minerals. However, everything that is instantisated is usually located outside of the camera view.

Could you please explain how I could make everything spawn relative to the camera's screen range? I apologize for the extremely long code.

The Item and the SpawnItem are just lists to store the objects to instantiate. So you can ignore them.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlanetGeneration : MonoBehaviour {
 
     public float itemSpacing = 15f;
 
     [Header("Map Size")]
     public float width = 100f;
     public float height = 100f;
 
     [Header("Sizes of items")]
     public float minPlanetSize = 10f;
     public float maxPlanetSize = 20f;
 
     public float minAsteroidSize = 5f;
     public float maxAsteroidSize = 10f;
 
     public float minMineralSize = 0.5f;
     public float maxMineralSize = 5f;
 
     [Header("Prefabs To Spawn")]
     public GameObject planetPrefab;
     public GameObject asteroidPrefab;
     public GameObject goldPrefab;
     public GameObject diamondPrefab;
 
     public List<SpawnItem> spawnItemList;
 
     void Start() {
         GenerateObjects ();
 
         StartCoroutine(SpawnPrefabsNearby());
     }
 
     IEnumerator SpawnPrefabsNearby () {
         while (true) {
             
             foreach (SpawnItem item in spawnItemList) {
                 
                 if (Mathf.Abs(item.pos.y - Warp.Position.y) < 30f) {
                     //This is to set the distance to which the items will spawn until
 
                     if (item.spawnedObject == null && !item.collected) {
                         item.spawnedObject = (GameObject)Instantiate(item.prefab, item.pos, Quaternion.identity, transform);
                         item.SetupSpawnedObject();
                     }
 
                 } else {
                     if (item.spawnedObject != null) {
                         Destroy(item.spawnedObject);
                     }
                 }
             }
 
             yield return new WaitForSeconds(2f);
         }
     }
 
     void GenerateObjects(){
         spawnItemList = new List<SpawnItem>();
 
         GenerateItems();
     }
 
     void GenerateItems () {
         
         float xAxis = 0f;
         while (xAxis < width) {
             
             float yAxis = 0f;
             int timeout = 0;
             while (yAxis < height) {
 
                 float multiplier = ((height - (yAxis - 1f)) / height) * 2f;
                 float realSpacing;
 
                 if (yAxis < height - 15f) {
                     realSpacing = itemSpacing * multiplier;
                 } else {
                     realSpacing = itemSpacing;
                 }
 
                 float xOffset = Random.Range(-itemSpacing / 2f, itemSpacing / 2f);
                 float yOffset = Random.Range(-realSpacing / 2f, realSpacing / 2f);
 
                 Vector3 pos = new Vector3(xAxis + xOffset - width / 2f, Mathf.Clamp(-yAxis - yOffset, -Mathf.Infinity, 1f), 0f);
                 RegisterSpawnItem(pos, yAxis);
                 
                 yAxis += realSpacing / 5f;    //Controls the Planet density
 
                 timeout++;
                 if (timeout > 10000)
                 {
                     Debug.LogError("TIMEOUT");
                     return;
                 }
             }
 
             xAxis += itemSpacing / 2f;
         }
     }
 
     void RegisterSpawnItem (Vector3 pos, float depth) {
         SpawnItem spawnItem = new SpawnItem ();
         spawnItem.pos = pos;
 
         spawnItem.prefab = planetPrefab;
         spawnItem.scale = Vector3.one * Random.Range (minPlanetSize, maxPlanetSize);
 
         spawnItemList.Add (spawnItem);
     }
 }
 
 

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

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

92 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

Related Questions

Procedurally generating cubes 1 Answer

My code is instantiating many prefabs, i only want one. 1 Answer

When re-parenting instantiated object as child of current gameObject, why does GetComponents() not retrieve instantiated object's components? 1 Answer

Generating prefabs at origin that are children of moving GameObjects? 2 Answers

scroll rect UI buttons all do the same thing 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