- Home /
A way to generate walls dynamically
I have a game where you can build your own place and stuff, you can make a custom layout of the place and all, the thing is, i can't figure out how to dynamically create the walls, i tried with Raycasts and if the ray detects another floor tile next to him then checks a bool indicating that can't generate a wall in that space:
void CheckForWalls()
{
RaycastHit hitU;
RaycastHit hitD;
RaycastHit hitL;
RaycastHit hitR;
if (!Physics.Raycast(new Vector3(box.bounds.center.x, box.bounds.center.y, box.bounds.max.z), transform.TransformDirection(Vector3.forward), out hitU, distance))
occupiedUp = true;
if (!Physics.Raycast(new Vector3(box.bounds.center.x, box.bounds.center.y, box.bounds.min.z), transform.TransformDirection(Vector3.back), out hitD, distance))
occupiedDown = true;
if (!Physics.Raycast(new Vector3(box.bounds.max.x, box.bounds.center.y, box.bounds.center.z), transform.TransformDirection(Vector3.left), out hitL, distance))
occupiedLeft = true;
if (!Physics.Raycast(new Vector3(box.bounds.min.x, box.bounds.center.y, box.bounds.center.z), transform.TransformDirection(Vector3.right), out hitR, distance))
occupiedRight = true;
}
I don't know if it's because the floors are detecting each other, but there are some occasions where the detection doesn't work really well. All kind of help is appreciated!
Answer by diamondgaming250 · Nov 21, 2020 at 02:51 AM
Nvmnd i got it. In case someone needs the code, here it is:
It will look for empty spaces to generate the walls later.
void CheckForWalls()
{
RaycastHit hitU;
RaycastHit hitD;
RaycastHit hitL;
RaycastHit hitR;
if (!Physics.Raycast(new Vector3(box.bounds.center.x, box.bounds.center.y, box.bounds.max.z), transform.TransformDirection(Vector3.forward), out hitU, distance, floorLayer))
notOccupiedUp = true;
else
notOccupiedUp = false;
if (!Physics.Raycast(new Vector3(box.bounds.center.x, box.bounds.center.y, box.bounds.min.z), transform.TransformDirection(Vector3.back), out hitD, distance, floorLayer))
notOccupiedDown = true;
else
notOccupiedDown = false;
if (!Physics.Raycast(new Vector3(box.bounds.max.x - 0.1f, box.bounds.center.y, box.bounds.center.z), transform.TransformDirection(Vector3.left), out hitL, distance, floorLayer))
notOccupiedLeft = true;
else
notOccupiedLeft = false;
if (!Physics.Raycast(new Vector3(box.bounds.min.x + 0.1f, box.bounds.center.y, box.bounds.center.z), transform.TransformDirection(Vector3.right), out hitR, distance, floorLayer))
notOccupiedRight = true;
else
notOccupiedRight = false;
}
Your answer
Follow this Question
Related Questions
Detecting game object under UI 2 Answers
Offseting insantiated object 1 Answer
Raycast bullet impact and instantiating particles 3 Answers
Help with moving instantiated object's transform 1 Answer
Capturing a point 2 Answers