Player builds a house with arbitrary walls, ceiling and floor
Player is inside the house
What is the best way to tell whether the player is inside or not?
Natural Disaster Survival has something akin to this in it’s sandstorm and rainstorm events, which play some GUI animations when outside. When the structures in the game begin to crumble, some places then quit being considered as “Inside” and become dangerous. This scenario is the opposite, with the “Inside” being constructed by a player. Any help?
You can do a few things. The easiest would be to raycast from the player upwards to see if it hits a ceiling. I wouldn’t know how the walls would work.
I believe that this would only work if the houses in question are prefabs. If the walls and floor and ceiling are arbitrarily put, then using this method might deduce the player is inside when they are not.
The green box is what the computer might think is “Inside”, but the player isn’t inside the house at all.
Look at the output! When it says “true”, it means I am inside, and when it says “false”, it means I’m outside!
Solution
First, I fire raycasts to all directions from the player character, with a max reach of 1000 studs. In this case, I’m firing a total of 26 raycasts every 1 second.
Then, if a cast hit something, I increment a value called the InsideCoefficient (measures “how inside” the character is), based on this formula:
The cast distance is used to weight the increment, since very far away objects might’ve been considered and increased drastically the InsideCoefficient unecessarily.
Then all you have to do is adjust the threshold and cast distance. In this case, I used 0.815 (actually it’s 815 since I’m multiplying by 1000 to get pretty numbers) and 1000 studs.
However, this method is very probably exploitable. I don’t care though, since it looks good enough.