1 min readFeb 4, 2022
Hello — thanks, I’m really happy you like the tutorial! :D
So, the idea is basically to avoid buildings “floating”, so we want to make sure that at least 3 of the 4 corners of their bounding box are on the ground. To do this, we use raycasting:
- we take the “phantom” building current position
p
- and we compute the 4 bounding box corners as offsets in
bottomCorners
(those areVector3
that describe the difference between the 4 “feet” of your building and the center of its bounding box) - finally, we iterate through those 4
bottomCorners
and, foreach, we cast a little ray down — but since we prepared offsets, we need to re-add thep
position to get the world equivalent position of each corner - these raycasts are done with
if(Physics.Raycast(...))
: if the ray hits the ground, then it returns “true”; else it returns “false” and the corner is “floating mid-air” => if we have more than one corner in the air, the placement is invalid!
I hope it helps clear things out, feel free to tell me if you have more questions! Cheers :)