- Home /
Making the player position coherent based on which door he got in
So i'm making a dungeon crawler with rooms generated in a procedural manner - each room has its directions, and when the player enters one of the doors, i want it to come out from the door he actually used. How should i approach that? I would love a thorough explanation on how to approach this, instead of "just" code. Helps me think and reason by myself :D i'll link the scripts i'm using down below :
Rooms:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class Room
{
public Vector2Int roomCoordinate;
public Dictionary<string, Room> neighbors;
public Room (int xCoordinate, int yCoordinate)
{
this.roomCoordinate = new Vector2Int(xCoordinate, yCoordinate);
this.neighbors = new Dictionary<string, Room>();
}
public Room (Vector2Int roomCoordinate)
{
this.roomCoordinate = roomCoordinate;
this.neighbors = new Dictionary<string, Room>();
}
public List<Vector2Int> NeighborCoordinates ()
{
List<Vector2Int> neighborCoordinates = new List<Vector2Int>();
neighborCoordinates.Add(new Vector2Int(this.roomCoordinate.x, this.roomCoordinate.y - 1));
neighborCoordinates.Add(new Vector2Int(this.roomCoordinate.x + 1, this.roomCoordinate.y));
neighborCoordinates.Add(new Vector2Int(this.roomCoordinate.x, this.roomCoordinate.y + 1));
neighborCoordinates.Add(new Vector2Int(this.roomCoordinate.x - 1, this.roomCoordinate.y));
return neighborCoordinates;
}
public void Connect (Room neighbor)
{
string direction = "";
if (neighbor.roomCoordinate.y <this.roomCoordinate.y)
{
direction = "N";
}
if (neighbor.roomCoordinate.x > this.roomCoordinate.x)
{
direction = "E";
}
if (neighbor.roomCoordinate.y > this.roomCoordinate.y)
{
direction = "S";
}
if (neighbor.roomCoordinate.x < this.roomCoordinate.x)
{
direction = "W";
}
neighbors.Add(direction, neighbor);
}
public string PrefabName()
{
string name = "Room_";
foreach (KeyValuePair<string, Room> neighborPair in neighbors)
{
name += neighborPair.Key;
}
return name;
}
public Room Neighbor(string direction)
{
return this.neighbors[direction];
}
}
Thanks in advance, i'm eager to hear how you would approach this
$$anonymous$$an...
IF you pretend to get an answer, make it easy.... Delete all unneccessary code, and post only the important... We will not read 225 lines of code...
Sure, sorry! i thought it'd help you understand
Answer by tormentoarmagedoom · Jun 06, 2019 at 10:49 AM
Hello.
Now, what i dont understand is what you need.
What you mean with "each room has its directions" and "come out from the door he actually used"
You mean if you left a room by the right side, character should enter into the next room by right side?
Then you only need to identify the doors position on the room, with a variable or using TAGs or calculate it using the transform position or rotation of the doors. And for example use trigger colliders to detect what door has been used, so you can have the information to spawn the caracter in the next room.
IF need something more specify, post some image or paint draw to understand what exactly you need:D
Bye!
Your answer
Follow this Question
Related Questions
enemy AI doesnt work when enemy is spawned but does if it is already in the scene 1 Answer
Problem with spawn player with RPC calls(c #) 1 Answer
Player Singleton and Spawning in new scene issue 0 Answers
Player only spawns upon play 1 Answer
How do you Place/Spawn a GameObject or Prefab in front of Player 2 Answers