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
1
Question by PoodLee · Mar 20, 2019 at 09:18 PM · randomlevelmaptower-defensegenerating

Generating random tower defense level by placing random square-shaped tiles?

Hey! What would be the best solution to generate a random tower defense game level? My game consists of square shaped pictures, like the different road elements etc. I was thinking of filling the scene with empty gameobjects, so it will look like a matrix, determine the start and end position, and fill up the empty gameobjects with the road elements so it will somehow form a path from the start point to the end point. Then it is easy to determie the waypoints the enemies follow, because whenever the algorithm puts down a road element, it will put down a waypoint as well. Is this a good idea? Or is there any better? Thanks in advance.

alt text

unityask.png (27.3 kB)
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
2

Answer by cs120319992 · Mar 21, 2019 at 06:20 PM

This is very simple code, and you need it edit

 public class Generator : Monobehaviour {
 public GameObject[,] allTiles = new GameObject[8, 6];
 
 public GameObject tileRoad;
 public GameObject otherTile;
 
 private void Generate() {
 // Fill map with other tiles
 for(int i = 0; i < allTiles.Length; i++) {
 for(int j = 0; j < allTiles[i].Length; j++) {
 GameObject other = Instantiate(otherTile)
 other.transform.position = new Vector2(i,j);
 }
 }
 }

  var dir = 0; // direction create til road 0 - up, 1 - down, 2 - left, 3 - right
 dir = Random.Range(0, 4);
 
 var pos = Vector2.zero;
 
 // For example create only 5 tiles
 for(int i = 0; i < 5; i++) {
 switch(dir) {
 case 0:
 pos.y+= 1;
 break;
 case 1:
 pos.y-= 1;
 break;
 case 2:
 pos.x= 1;
 break;
 case 3:
 pos.x+= 1;
 break;
 }

 GameObject other = Instantiate(tileRoad)
 pos.y+= 1;
 other.transform.position = new Vector2(pos.x, pos.y);
 }
 }





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

109 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

Related Questions

Making the floor/ground of a map 1 Answer

I need a map/level editor! 0 Answers

Procedural level generation for a 3D side-scroller 2 Answers

Unity crashes with my function for random generating paths 2 Answers

Load a random scene from a list of strings. 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