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 EncryptedCow · May 17, 2013 at 03:56 AM · javascriptrandompathgeneration

Generate random path in grid

I'm trying to generate a random path in a 3x3 grid and don't know where to start. What I want to do is have it always start in the bottom center room and end in the top center room, and set values in an array to a different number depending on where there needs to be doors depending on the path. (eg. top-left room (number 0 in array) gets set to 3 because it needs to have a door on the south wall and the east wall. another function then reads those variables to know what kind of room it needs to build) Currently I have made paths that it will choose from but obviously this can get figured out easily. How would I go about doing this so that no rooms are left out?

Comment
Add comment · Show 1
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 robertbu · May 17, 2013 at 01:51 PM 0
Share

If you final goal is a 3x3 grid (or even a 4x4 grid), then consider statically initializing a 2D (Jagged) array. Number your rooms 0 through 8 and then work out various paths. There are not that many with a 3x3 grid. Then just declare an array of these paths. You can use Random.Range() to select between them.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by codecranker · May 17, 2013 at 04:09 AM

Since you already fixed the start and end points for your path that makes things lot less simple.

you can do something like the following:

  1. chose a start point. let this be your current position.

  2. get a list of all reachable positions from the current position in all valid directions. (these positions should be the grid-cells adjacent to the current position)

  3. select a random position from the list

  4. move to the new position

  5. if the new position is the end position, return

  6. goto 2

you might end up with longer paths sometime because you are choosing your next positions randomly. if you want to be sure that your path should not get longer beyond a certain length, you can put some logic in choosing your next position from the list of reachable positions. For eg; select a random position from the list whose distance from the end point is within a certain range.

Comment
Add comment · Show 3 · 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 EncryptedCow · May 17, 2013 at 04:30 AM 0
Share

I can see how this would work, but I am doing this with restrictions. With this method it could easily go up, down, then left and get stuck.

avatar image Bunnybomb7670 · May 17, 2013 at 10:23 AM 0
Share

Or you could ins$$anonymous$$d use that system which codecranker said but also store already explored positions aswell as checking if the sector your going to move to has possible positions available to move ahead to.

avatar image codecranker · May 17, 2013 at 03:08 PM 0
Share

well it depends how complicated you wanna get. the one I mentioned is just high level. As bunnybomb7670 said you can record already explored positions or you can also set repeat count for every position i.e. how many times is it O$$anonymous$$ for your algorithm to repeat a position in your path without raising a red flag.

The step (2) in a algorithm I mentioned takes care of finding "all reachable positions from the current position in all valid directions.". You can always add more validations to it.

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

15 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

Related Questions

Generating random paths 1 Answer

Perform Function, If Not Clicked In 3 Seconds 2 Answers

Code to randomly generate a mesh? 0 Answers

How to move an object on a random path with iTween 1 Answer

Sound playing at random. (JS) 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