- Home /
how would I use teleport in in procedural generated rooms
OK To start I would like the rooms to be far away from each other. I want to make it so when u walk from one door you teleport to the door it is 'connected' to. How would I do this in procedurally generated rooms?
You need to know the doors positions. Is there any way to find those positions after creating the rooms?
That was what I was thinking but there Is no way to know where the door is. How would I add that?
Answer by Tuttle321 · Apr 12, 2021 at 08:10 PM
Ok, so there is a lot.
public enum Direction { up = 0, left = 1, down = 2, right = 3 };
public class dungeonCrawlerController : MonoBehaviour
{
public static List<Vector2Int> positionsVisted = new List<Vector2Int>();
private static readonly Dictionary<Direction, Vector2Int> directionMovementMap = new Dictionary<Direction, Vector2Int>
{
{Direction.up, Vector2Int.up},
{Direction.left, Vector2Int.left},
{Direction.down, Vector2Int.down},
{Direction.right, Vector2Int.right}
};
public static List<Vector2Int> GenerateDungeon(genData dungeonData)
{
List<dungeonCrawler> dungeonCrawlers = new List<dungeonCrawler>();
for (int i = 0; i < dungeonData.numberOfCrawlers; i++)
{
dungeonCrawlers.Add(new dungeonCrawler(Vector2Int.zero));
}
int iterations = Random.Range(dungeonData.iterationMin, dungeonData.iterationMax);
for(int i = 0; i < iterations; i++)
{
foreach(dungeonCrawler dungeonCrawler in dungeonCrawlers)
{
Vector2Int newPos = dungeonCrawler.Move(directionMovementMap);
positionsVisted.Add(newPos);
}
}
return positionsVisted;
}
}
This is the Dungeon Crawler Controller.
public class generator : MonoBehaviour { public genData GenData;
private List<Vector2Int> dungeonRooms;
private void Start()
{
dungeonRooms = dungeonCrawlerController.GenerateDungeon(GenData);
SpawnRooms(dungeonRooms);
}
private void SpawnRooms(IEnumerable<Vector2Int> rooms)
{
roomController.instance.loadRoom("Start", 0, 0);
foreach (Vector2Int roomLocation in rooms)
{
roomController.instance.loadRoom("Empty", roomLocation.x, roomLocation.y);
}
}
} This is the generator.
public class room : MonoBehaviour { public int width; public int Height;
public int X;
public int Y;
void Start()
{
if (roomController.instance == null)
{
Debug.Log("U Ducked Up");
return;
}
roomController.instance.registerRoom(this);
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireCube(transform.position, new Vector3(width, Height));
}
public Vector3 getRoomCenter()
{
return new Vector3 (X * width, Y * Height);
}
} I think I need to add something here. This is the room script.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class roomInfo { public string name; public int X; public int Y; }
public class roomController : MonoBehaviour {
public static roomController instance;
string currentWorldName = "Basement";
roomInfo currentloadRoomData;
Queue<roomInfo> loadRoomQueue = new Queue<roomInfo>();
public List<room> loadedRooms = new List<room>();
bool isLoadingRoom = false;
void Awake()
{
instance = this;
}
void Update()
{
UpdateRoomQueue();
}
void UpdateRoomQueue()
{
if (isLoadingRoom)
{
return;
}
if (loadRoomQueue.Count == 0)
{
return;
}
currentloadRoomData = loadRoomQueue.Dequeue();
isLoadingRoom = true;
StartCoroutine(loadRoomRoutine(currentloadRoomData));
}
public void loadRoom( string name, int x, int y)
{
if (doesRoomExist(x, y))
{
return;
}
roomInfo newRoomData = new roomInfo();
newRoomData.name = name;
newRoomData.X = x;
newRoomData.Y = y;
loadRoomQueue.Enqueue(newRoomData);
}
IEnumerator loadRoomRoutine(roomInfo info)
{
string roomName = currentWorldName + info.name;
AsyncOperation loadRoom = SceneManager.LoadSceneAsync(roomName, LoadSceneMode.Additive);
while(loadRoom.isDone == false)
{
yield return null;
}
}
public void registerRoom( room room)
{
room.transform.position = new Vector3 (
currentloadRoomData.X * room.width,
currentloadRoomData.Y * room.Height,
0
);
room.X = currentloadRoomData.X;
room.Y = currentloadRoomData.Y;
room.name = currentWorldName + "-" + currentloadRoomData.name + " " + room.X + ", " + room.Y;
room.transform.parent = transform;
isLoadingRoom = false;
loadedRooms.Add(room);
}
public bool doesRoomExist ( int x, int y)
{
return loadedRooms.Find( item => item.X == x && item.Y == y) != null;
}
} This is the Room Controller.
public class dungeonCrawler : MonoBehaviour { public Vector2Int Position { get; set;}
public dungeonCrawler(Vector2Int startPos)
{
Position = startPos;
}
public Vector2Int Move(Dictionary<Direction, Vector2Int> directionMovementMap)
{
Direction toMove = (Direction)Random.Range (0, directionMovementMap.Count);
Position += directionMovementMap[toMove];
return Position;
}
}
This is the last one. hope u can help! @sath
https://www.youtube.com/watch?v=ClKUoD5J7mo&t=11s link to a part of the vid when he talks about doors. Have not added this yet.
@sath ^^^^ Thanks so much for the help. Sorry if this is a lot!
ok this video is the solution to your problem. At some point in Room.cs you can have access to the room's doors as gameobjects, so you can know the positions.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032147im_/https://answers.unity.com/themes/thub/images/avi.jpg)