• 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 Andrew Chami · Dec 24, 2014 at 05:12 PM · c#randomspawnroomdungeon

Why is this room always spawning on left side?

Hey. I've been coding this dungeon generator and the boss room (the very white room), is always spawning on the right. Here are screenshots of different instances. I mostly get between those two rooms even if i regenerate the dungeon 20 times.

Code:

using UnityEngine; using System.Collections;

public class SpawnRooms : MonoBehaviour {

 int spawnerID;

 public int maxXDEFAULT;
 public int maxYDEFAULT;
 public int dungeonSizeXDEFAULT;
 public int dungeonSiZeYDEFAULT;

 public GameObject room1;
 public GameObject room2;
 public GameObject room3;

 public int DungeonID;

  bool shouldSpawnBossRoom;


 void Start () {
     shouldSpawnBossRoom = true;
     InstantiateRooms (maxXDEFAULT,maxYDEFAULT,dungeonSizeXDEFAULT,dungeonSiZeYDEFAULT);
 }
 
 void InstantiateRooms(int maxX, int maxY, int dungeonSizeX,int dungeonSizeY){
     for(int iX =0; iX<maxX; iX+= dungeonSizeX ){
         for(int iY = 0; iY< maxY; iY+= dungeonSizeY){

             CheckWhatToSpawn(iX,iY);


         }
     }
 }


 void CheckWhatToSpawn(int x, int y){
     if (shouldSpawnBossRoom == true) {
                     DungeonID = Random.Range (1, 4);
             } else {
         DungeonID = Random.Range(2,4);    
     }
     if (DungeonID == 1 && shouldSpawnBossRoom == true) {
                     shouldSpawnBossRoom = false;
                     GameObject instance1 = (GameObject)Instantiate (room1, new Vector3 (x, y, 0), Quaternion.identity);
                     instance1.name = "Boss Room";
             } 
     
     
     if (DungeonID == 2) {
         GameObject instance2 = (GameObject)Instantiate (room2, new Vector3(x,y,0), Quaternion.identity);
         instance2.name = "Room2";
         
     } 
     
     
     if(DungeonID == 3 ){
         GameObject instance3 = (GameObject)Instantiate(room3,new Vector3(x,y,0),Quaternion.identity);
         instance3.name = "Room3";
         
     }


 }
 

}

alt text

alt text

instance1.png (45.6 kB)
instance2.png (49.1 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
0
Best Answer

Answer by RudyTheDev · Dec 24, 2014 at 05:17 PM

You are generating rooms sequentially through y and then x coordinates (for x, for y ..). So new rooms are created down first column, then down second, etc. Your boss room selection is the first time a random number is 1..4. After that you disable boss room spawning. So one would expect on average to see it about 70% (4/5 ^5) in the first column, which is what your observations seem to match.

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 sdsanft · Dec 24, 2014 at 05:24 PM 0
Share

I'm pretty new, but perhaps would randomly choosing two random numbers (random column and random row) and then spawning the boss room at those coordinates and then working out from there be better?

avatar image Andrew Chami · Dec 24, 2014 at 05:47 PM 0
Share

Thanks a lot Rudy. Your answer was perfect :D

avatar image RudyTheDev · Dec 24, 2014 at 11:12 PM 0
Share

What sdsanft suggests is probably the most straight-forward way to ensure the boss room is truly randomly placed. It does involve a different approach that the current one, as you'd have to remember where you spawned the boss room (or anything else, for that matter) besides "regular" rooms.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to Spawn an Extra Platform for the Player to Jump On If There Are None Within the Player's Field of View? 0 Answers

C# Random spawn from an array 0 Answers

random spawning 1 Answer

Random element from the list 1 Answer

Distribute terrain in zones 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges