The question is answered, right answer was accepted
Why does this code run infinitely?
From what I can see, this code should only run 20 time but it keeps generating rooms forever.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RoomGeneration : MonoBehaviour {
public int roomsGenerated; // How many rooms are generated
public GameObject[] rooms;
public GameObject doorTop;
public GameObject doorBottom;
public GameObject doorLeft;
public GameObject doorRight;
public GameObject rayUp;
public GameObject rayDown;
public GameObject rayLeft;
public GameObject rayRight;
int genX; // Room X coord
int genY; // Room Y coord
// Use this for initialization
void Start()
{
genX = 0;
genY = 0;
Instantiate(rooms[0], new Vector2(genX, genY), Quaternion.identity);
}
// Update is called once per frame
void Update()
{
for(int i = 0; i <= 20; i++)
{
CheckAndBuild();
}
}
void CheckAndBuild()
{
int randomRoomDir = Random.Range(0, 4);
int randomRoom = Random.Range(1, 6);
RaycastHit2D hitUp = Physics2D.Raycast(rayUp.transform.position, transform.up, 3f);
RaycastHit2D hitDown = Physics2D.Raycast(rayDown.transform.position, transform.up * -1, 3f);
RaycastHit2D hitLeft = Physics2D.Raycast(rayLeft.transform.position, transform.right * -1, 5f);
RaycastHit2D hitRight = Physics2D.Raycast(rayRight.transform.position, transform.right, 5f);
Debug.DrawRay(rayUp.transform.position, transform.up * 3f, Color.red, 1000f);
Debug.DrawRay(rayDown.transform.position, transform.up * -1 * 3f, Color.red, 1000f);
Debug.DrawRay(rayLeft.transform.position, transform.right * -1 * 5f, Color.red, 1000f);
Debug.DrawRay(rayRight.transform.position, transform.right * 5f, Color.red, 1000f);
if (randomRoomDir == 0 && hitUp.collider == null) // Generate room above
{
Instantiate(doorTop, new Vector2(genX, genY + 3.55f), Quaternion.identity);
genY += 10;
Vector2 roomGenPos = new Vector2(genX, genY);
transform.position = roomGenPos;
Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
Instantiate(doorBottom, new Vector2(genX, genY - 3.55f), Quaternion.identity);
print(hitUp.collider);
}
if (randomRoomDir == 1 && hitDown.collider == null) // Generate room below
{
Instantiate(doorBottom, new Vector2(genX, genY - 3.55f), Quaternion.identity);
genY -= 10;
Vector2 roomGenPos = new Vector2(genX, genY);
transform.position = roomGenPos;
Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
Instantiate(doorTop, new Vector2(genX, genY + 3.55f), Quaternion.identity);
print(hitUp.collider);
}
if (randomRoomDir == 2 && hitLeft.collider == null) // Generate room to the left
{
Instantiate(doorLeft, new Vector2(genX - 7.4f, genY), Quaternion.identity);
genX -= 20;
Vector2 roomGenPos = new Vector2(genX, genY);
transform.position = roomGenPos;
Instantiate(rooms[randomRoom], roomGenPos, Quaternion.identity);
Instantiate(doorRight, new Vector2(genX + 7.4f, genY), Quaternion.identity);
print(hitUp.collider);
}
if (randomRoomDir == 3 && hitRight.collider == null) // Generate room to the right
{
Instantiate(doorRight, new Vector2(genX + 7.4f, genY), Quaternion.identity);
genX += 20;
Instantiate(rooms[randomRoom], new Vector2(genX, genY), Quaternion.identity);
Instantiate(doorLeft, new Vector2(genX - 7.4f, genY), Quaternion.identity);
print(hitUp.collider);
}
}
}
Also, my raycasts aren't hitting any colliders. There are four raycasts (top, left, right and bottom) that are meant to check if there is a room there and if there isn't, it can generate a room there. It doesn't seem to detect any rooms though so rooms are spawned on top of other rooms sometimes.
Update() is called every frame. You probably want to move the initialization code to Start():
Yeah but shouldn't CheckAndBuild() stop running once i = 20?
Or is i being created and set to 0 every frame?
Follow this Question
Related Questions
Why aren't raycasts hitting any colliders? 0 Answers
Problem with random room gen using raycasts! 0 Answers
RayCastHit2D fails to hit script generated grid 1 Answer
Stucking with RTS Building detection using 2D Raycasts and 2DBoxColliders 0 Answers
Vertical Rays rapidly changing between "detecting ground" to "not detecting ground" 0 Answers