• 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 PaintedBirds · May 21, 2020 at 05:22 PM · c#instantiateprocedural

Dungeon Generator problems

I am trying to make a Dungeon following a perlin noise cluster, but only in 1 cluster, not the whole room. so I made a self duplicating stucture that should delete themselfs after spreading, and should die out after expanding to the size of a cluster.

But for some reason, I get an infinite loop. It doubles back on itsef, and does not get deleted once it does that.

Here is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RoomGen : MonoBehaviour
 {
     public float RoomValue;
     public float seed = 1.457582f;
     public GameObject Marker;
     public GameObject Self;
     // Start is called before the first frame update
     void Start()
     {
     }
     void LateUpdate()
     {
         RoomValue = Mathf.PerlinNoise(transform.position.x * seed, transform.position.z * seed);
         if (RoomValue >= .4f)
         {
             Instantiate(Marker, transform.position, transform.rotation);
             Instantiate(Self, transform.position + new Vector3(-1, 0 ,0), transform.rotation);
             Instantiate(Self, transform.position + new Vector3(1, 0, 0), transform.rotation);
             Instantiate(Self, transform.position + new Vector3(0, 0, -1), transform.rotation);
             Instantiate(Self, transform.position + new Vector3(0, 0, 1), transform.rotation);
             Destroy(this.gameObject);
         }
         else
         {
             Destroy(this.gameObject);
         }
     }
     
     
     private void OnCollisionStay(Collision collision)
     {
         Destroy(this.gameObject);
     }
     
 }

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 Eno-Khaon · May 22, 2020 at 06:09 PM

First off, looking at your seed value and your positions used, it's worth noting that Unity's Perlin noise function repeats with every value of 1.0. In a worst-case scenario for what you're doing (a seed value of 1), you would have the same value from the noise function in every case.

However, your problem lies in the fact that you're creating objects on all four sides with no safety in place. If any two adjacent spaces both result in a noise value of 0.4 or higher, they will recreate each other endlessly.

Possible (not necessarily robust) solutions include modifying the seed any time the new objects are generated, or keeping track of a simple list of "used locations" to prevent remaking an object at a location that's been used previously.

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 PaintedBirds · May 22, 2020 at 07:05 PM 0
Share

@Eno-Khaon From what you are saying, I will reduce my seed to be below 0.5, for larger dungeons. I was hoping the collision detection would keep 2 adjectent points from replicating endlessly, as touching another object should delete the spawning object, and that is my biggest problem, in theory the collision should work as a physical "used locations" list, preventing the need for one in every object. The problem is LateUpdate(), specificly chosen to try and prevent problems, is happening before the OnCollisionStay() Occurs.

avatar image Eno-Khaon PaintedBirds · May 22, 2020 at 09:44 PM 0
Share

I mention the seed values and Unity's Perlin Noise implementation repeating more for information than much else. Whether you want the smoothed or noisier pattern (based on separation between values) is entirely at your discretion.

Because LateUpdate() is relative to framerate and OnCollisionStay() is relative to physics, it's unreasonable to rely on them alternating, whether they can or not. Additionally, depending on the exact timing and state of objects, they wouldn't be found regardless, because you're destroying the one creating the others.

avatar image PaintedBirds Eno-Khaon · May 23, 2020 at 05:24 PM 0
Share

So would it be better to try and detect a collision withing LateUpdate() so everything is in sync? Or to create 1 long shared list of previous points, deleting any that go back onto used areas?

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

133 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

UI Instantiate?,Ui, Sumonar UI? 0 Answers

Why is Prefab declared as type Transform? 2 Answers

Instantiation problem 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