Question by
Caerex · Mar 10, 2016 at 03:51 PM ·
c#generationwall
How to generate the Top and Right border for my game's arena?
Hello, I am relatively new to C# and Unity as a whole and am making a game involving randomly generated boards of different sizes. However when I generate the board the Left and Bottom edge generate fine whereas the Top and Right do not generate at all.
I believe the error is located in this line:
if ( x == -2.23f || x == (columns * 2.23f) ||y == -2.23f || y == (rows * 2.23f))
I am using this Unity tutorial as a guideline.
Here is the full code:
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using Random = UnityEngine.Random;
public class BoardManager : MonoBehaviour {
[Serializable]
public class Count
{
public int minimum;
public int maximum;
public Count (int min, int max)
{
minimum = min;
maximum = max;
}
}
//Number of Columns on board
public int columns = 5;
//Number off Rows on board
public int rows = 4;
//Number of inner walls to be made between (x, y)
public Count wallCount = new Count(9,11);
public GameObject[] floorTiles;
public GameObject[] innerWallTiles;
public GameObject[] outerWallTiles;
private Transform boardHolder;
private List<Vector3> gridPositions = new List<Vector3>();
//2.23 is the size of each tile
void InitialiseList()
{
//Empties previous grid positions
gridPositions.Clear();
//For each Column.....
for (float x = 2.23f; x < (columns*2.23f) -2.23f; x+=2.23f) {
//...Do each Row....
for (float y = 2.23f; y < (rows*2.23f) -2.23f; y+=2.23f) {
//... To create a List of possible positions for each tile
gridPositions.Add(new Vector3(x, y, 0f));
}
}
}
//Creates "Canvas" with outer wall and background of floor
void BoardSetUp()
{
boardHolder = new GameObject("Board").transform;
//Goes to columns +-2.23 is to create edge
for (float x = -2.23f; x <(columns*2.23f) + 2.23f; x+=2.23f){
//Goes to rows +-2.23 to create edge
for (float y = -2.23f; y <( rows*2.23f) + 2.23f; y+=2.23f){
//Randomly chooses a Floor tile design (Allows more to be added later due to length not being specified)
GameObject toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)];
//Checks whether it is the games border
if ( x == -2.23f || x == (columns * 2.23f) ||y == -2.23f || y == (rows * 2.23f))
{
//Picks a random outer wall tile to instantiate
toInstantiate = outerWallTiles[Random.Range(0, outerWallTiles.Length)];
}
//Creates tile at correct coordinates (x,y,0) with no rotation (quaternion) as a new gameobject
GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity) as GameObject;
//Sets parent as board Holder for easy management
instance.transform.SetParent(boardHolder);
}
}
}
Vector3 RandomPosition()
{
//Selects random positions
int randomIndex = Random.Range(0, gridPositions.Count);
Vector3 randomPosition = gridPositions[randomIndex];
//Removes the Position
gridPositions.RemoveAt(randomIndex);
//Returns the value random position
return randomPosition;
}
void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
{
//Controls how many of an object spawn
int objectCount = Random.Range(minimum, maximum + 1);
//For the number in objectCount.....
for (int i=0; i < objectCount; i++)
{
Vector3 randomPosition = RandomPosition();
//Picks a Tile
GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];
//Creates The tile
Instantiate(tileChoice, randomPosition, Quaternion.identity);
}
}
//Public so can be accessed outside of file by GameManager script
public void SetupScene()
{
BoardSetUp();
InitialiseList();
//Lays out the tiles
LayoutObjectAtRandom(innerWallTiles, wallCount.minimum,wallCount.maximum);
}
}
Thank you for helping me.
Comment
Your answer
Follow this Question
Related Questions
Procedural Cave Generation Can´t understand?? 0 Answers
Can you use the superformula in unity? 0 Answers
C# Unity - Roguelike room generation HELP 1 Answer
How to add programatically animationClips to one animation from Resources folder? 0 Answers
Understanding Procedural Level Generation A LA Alto's Adventure 0 Answers