How can I detect if a part is inside of terrain?

  1. What do you want to achieve? Keep it simple and clear!
    I want to detect if a part is inside of terrain

  2. What is the issue? Include screenshots / videos if possible!
    Im not sure how to do it.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried YT and DevForum.

4 Likes

Not sure exactly what the part is or what the terrain looks like, but if you have flat terrain you can determine it’s Y value and get the position of the part and compare it if it’s less than (<) terrain Y value.

If this is not what you’re looking for, could you elaborate more about the part and what you’re trying to achieve?

2 Likes

Its just a normal part and my terrain is not flat, so how could I achieve my goal?

1 Like

You could maybe use a raycast? Do you want tk onow if its fully covered or just partially? If its partislly, use a shapecast.

2 Likes

I’m using raycast rn (im making a placement system) so how could I detect if my viewmodel is inside terrain?

1 Like

You can use Terrain:ReadVoxels().

It’s pretty complicated to learn it, but it can super beneficial once you learn it.

Raycasts also work.

2 Likes

You could try detecting it using the terrain size, something like this:

local terrain = game.Workspace.Terrain
local terrainSize = terrain.Size

local part = game.Workspace.PartToCheck
local partPosition = part.Position

local minX = -terrainSize.X / 2
local minY = 0
local minZ = -terrainSize.Z / 2

local maxX = terrainSize.X / 2
local maxY = terrainSize.Y
local maxZ = terrainSize.Z / 2

if (partPosition.X >= minX and partPosition.X <= maxX) and
   (partPosition.Y >= minY and partPosition.Y <= maxY) and
   (partPosition.Z >= minZ and partPosition.Z <= maxZ) then
   -- The part is inside the terrain
   print("The part is inside the terrain.")
else
   -- The part is outside the terrain
   print("The part is outside the terrain.")
end
2 Likes

maybe you could also detect with collision, if it is colliding with terrain and take actions before hand.

2 Likes

Have you tried looking at other DevForum posts?

2 Likes

I already looked at that and it didn’t help me

1 Like