How to tell if a player is inside a room?

Consider the scenario:

Player is outside
image

Player builds a house with arbitrary walls, ceiling and floor
image

Player is inside the house
image

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.

Maybe sideways raycasts? Diagonal raycasts? Not sure how many is too many, but it does sound like a good option.

I feel like using regions would be your best bet honestly

You can use the spatial query API as well. Not sure how you’d use it in this context though.

What I would do is find the players position and see if it is in the house or not. I can give you code if you want.

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.

image
The green box is what the computer might think is “Inside”, but the player isn’t inside the house at all.

I believe that this might be solved if you use an upwards raycast. But it depends if you want to consider them as inside if they’re just under a roof.

I got a solution, and it works well.

Sorry for low resolution, am poor

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:

Increment = (1 / TotalCastsFired) * (1 - (CastDistance / 1000))

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.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.