Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 nursedayuksel · Nov 10, 2020 at 04:13 PM · 2d-platformerspawntilemaprandomspawningrandom spawn

Spawning prefabs randomly on top of existing tiles

Good day everyone!

I'm currently in the process of building a 2D Game, and as for my next step I'd like to randomly spawn collectable gems (prefabs) on top of my tiles for the player to collect.

My tilemap is generated at runtime.

I've tried various solutions however I can't seem to get it quite right. I know that I'm supposed to get the positions of the tiles and check whether the tile below is empty or not, however I'm not sure how to implement this, and I'd really appreciate the help! 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by nursedayuksel · Nov 10, 2020 at 09:03 PM

Here's my code for generating the tilemap incase anyone would like to see it.

     void GenerateMap()
     {
         float seed = UnityEngine.Random.Range(.1f, 1f);
         mapArray = GenerateArray(terrainWidth, terrainHeight, true);
         RenderMap(PerlinNoiseSmooth(mapArray, seed, 3), tilemap, tile);
     }
 
 
     public static int[,] GenerateArray(int width, int height, bool empty)
     {
         int[,] map = new int[width, height];
         for (int x = 0; x < map.GetUpperBound(0); x++)
         {
             for (int y = 0; y < map.GetUpperBound(1); y++)
             {
                 if (empty)
                 {
                     map[x, y] = 0;
                 }
                 else
                 {
                     map[x, y] = 1;
                 }
             }
         }
         return map;
     }
 
     public static void RenderMap(int[,] map, Tilemap tilemap, TileBase tile)
     {
         //Clear the map (ensures we dont overlap)
         tilemap.ClearAllTiles();
         //Loop through the width of the map
         for (int x = 0; x < map.GetUpperBound(0); x++)
         {
             //Loop through the height of the map
             for (int y = 0; y < map.GetUpperBound(1); y++)
             {
                 // 1 = tile, 0 = no tile
                 if (map[x, y] == 1)
                 {
                     tilemap.SetTile(new Vector3Int(x, y, 0), tile);
 
                 }
             }
         }
     }
 
     public static void UpdateMap(int[,] map, Tilemap tilemap) //Takes in our map and tilemap, setting null tiles where needed
     {
         for (int x = 0; x < map.GetUpperBound(0); x++)
         {
             for (int y = 0; y < map.GetUpperBound(1); y++)
             {
                 //We are only going to update the map, rather than rendering again
                 //This is because it uses less resources to update tiles to null
                 //As opposed to re-drawing every single tile (and collision data)
                 if (map[x, y] == 0)
                 {
                     tilemap.SetTile(new Vector3Int(x, y, 0), null);
                 }
             }
         }
     }
 
     public static int[,] PerlinNoise(int[,] map, float seed)
     {
         int newPoint;
         //Used to reduced the position of the Perlin point
         float reduction = 0.5f;
         //Create the Perlin
         for (int x = 0; x < map.GetUpperBound(0); x++)
         {
             newPoint = Mathf.FloorToInt((Mathf.PerlinNoise(x, seed) - reduction) * map.GetUpperBound(1));
 
             //Make sure the noise starts near the halfway point of the height
             newPoint += (map.GetUpperBound(1) / 2);
             for (int y = newPoint; y >= 0; y--)
             {
                 map[x, y] = 1;
             }
         }
         return map;
     }
 
     public static int[,] PerlinNoiseSmooth(int[,] map, float seed, int interval)
     {
         //Smooth the noise and store it in the int array
         if (interval > 1)
         {
             int newPoint, points;
             //Used to reduced the position of the Perlin point
             float reduction = 0.5f;
 
             //Used in the smoothing process
             Vector2Int currentPos, lastPos;
             //The corresponding points of the smoothing. One list for x and one for y
             List<int> noiseX = new List<int>();
             List<int> noiseY = new List<int>();
 
             //Generate the noise
             for (int x = 0; x < map.GetUpperBound(0); x += interval)
             {
                 newPoint = Mathf.FloorToInt((Mathf.PerlinNoise(x, (seed * reduction))) * map.GetUpperBound(1));
                 noiseY.Add(newPoint);
                 noiseX.Add(x);
             }
 
             points = noiseY.Count;
 
             //Start at 1 so we have a previous position already
             for (int i = 1; i < points; i++)
             {
                 //Get the current position
                 currentPos = new Vector2Int(noiseX[i], noiseY[i]);
                 //Also get the last position
                 lastPos = new Vector2Int(noiseX[i - 1], noiseY[i - 1]);
 
                 //Find the difference between the two
                 Vector2 diff = currentPos - lastPos;
 
                 //Set up what the height change value will be 
                 float heightChange = diff.y / interval;
                 //Determine the current height
                 float currHeight = lastPos.y;
 
                 //Work our way through from the last x to the current x
                 for (int x = lastPos.x; x < currentPos.x; x++)
                 {
                     for (int y = Mathf.FloorToInt(currHeight); y > 0; y--)
                     {
                         map[x, y] = 1;
                     }
                     currHeight += heightChange;
                 }
             }
         }
         else
         {
             //Defaults to a normal Perlin gen
             map = PerlinNoise(map, seed);
         }
 
         return map;
     }
  
 }
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
avatar image
0

Answer by Shelia_P · Nov 12, 2020 at 11:32 AM

Thanks for the solution

Nox

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

156 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Spawning a specific Tile in a random position a certain distance from the player character 1 Answer

Destroy tile at impact (similar to Broforce)? 1 Answer

Random Map generation like Temple run,Random Map generation like Temple Run. 0 Answers

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

Trouble with tilemaps as prefabs for 2D plattformer 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