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 nursedayuksel · Nov 09, 2020 at 02:42 PM · unity 2dspawnprefabstilemapplatformer

How to spawn multiple prefabs - Tilemap

Good day everyone! I'm trying to build my second game in Unity and it's a 2d platformer game.

I successfully managed to randomly generate my scene with Tilemaps using a script.

As my next task, I want to spawn gems (Collectible prefabs) at random locations in my scene, and I want them to spawn above my tiles so they wont overlap. How can I do this? I've approached this using different methods, however I was successful in creating multiple spawns, so I'd really appreciate the help. :)

I'll attach my script below for generating my scene:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class GenerateTerrain : MonoBehaviour
 {
     int[,] mapArray;
 
     Tilemap tilemap;
 
     public TileBase tile;
 
 
     public int terrainWidth, terrainHeight;
 
     // Start is called before the first frame update
     void Start()
     {
         tilemap = GetComponent<Tilemap>();
         GenerateMap();
 
 
     }
 
     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
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

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

Related Questions

Having issue with enemy ai shooting 0 Answers

Problem: Random Instantiating more than one prefab? 1 Answer

How do you rotate a tile? 1 Answer

Tilemap Glitching 0 Answers

Serious performance issues 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