c# – Placing a Prefab in Unity
So I am making a proceduraly generated dungeon, Ive got a algo that creates base of the dungeon out of cubes, it creates rooms, hallways and staircases. Once I had that I started to place prefabs on it (walls, floors ceilings etc.). I got stuck on a staircases, so basically Ive got 4 cubes (1×1) making a rectangle(2x2x1) and I need to place a prefab of a staircase on them, but I just cant do IT
How I am currently placing a prefab
if (delta.y != 0)
{
int xDir = Mathf.Clamp(delta.x, -1, 1);
int zDir = Mathf.Clamp(delta.z, -1, 1);
Vector3Int verticalOffset = new Vector3Int(0, delta.y, 0);
Vector3Int horizontalOffset = new Vector3Int(xDir, 0, zDir);
// Mark the grid as containing stairs
grid[prev + horizontalOffset] = CellType.Stairs;
grid[prev + horizontalOffset * 2] = CellType.Stairs;
grid[prev + verticalOffset + horizontalOffset] = CellType.Stairs;
grid[prev + verticalOffset + horizontalOffset * 2] = CellType.Stairs;
// Place the green cubes for stairs (this places them in the grid)
PlaceStairs(prev + horizontalOffset);
PlaceStairs(prev + horizontalOffset * 2);
PlaceStairs(prev + verticalOffset + horizontalOffset);
PlaceStairs(prev + verticalOffset + horizontalOffset * 2);
// Calculate the position of the stairs prefab
Vector3 stairPosition = new Vector3(prev.x + horizontalOffset.x, prev.y, prev.z + horizontalOffset.z);
stairPosition += new Vector3(0.5f, 0.5f, 0.5f);
Instantiate(stairPrefab, stairPosition, Quaternion.identity);
}
this is the part of my code when the algo detects a change in elevation and places a staircase. It marks the 4 cubes as stairs cells and then uses PlaceStairs() to place the green cubes.
How do I get the direction in which the stairs are going and how do I place it inside the green cubes. Any help would be greatly appreciated <3
Read more here: Source link