RayCast and terrain detection

Hello, im working on island generation and i have some problems, trees grow on rocks…

Im using raycasting to detect Yposition for tree but i also have to know what material is.
code i used so far:

function getSurfacePosition(posX, posZ)
	local origin = Vector3.new(posX, 500, posZ)
	local target = Vector3.new(posX, -100, posZ)
	local ray = Ray.new(origin, (target - origin).Unit * 750) 
	local hit, pos, material = workspace:FindPartOnRayWithIgnoreList(ray, workspace.Folder:GetChildren(), false, true)

	return pos, hit, material

on output i just get terrain

local surfacePos, material = getSurfacePosition(posX+RandomposX, posZ+RandomposZ)

Can someone help?


Have you tried using parts rather than terrain?

1 Like

no and i dont plan on doing it because i want to give players tools to edit terrain

ah yes read voxels, most broken command of all time, i tried but cant get it working, i will try again

still cant get it working, i used this:

function printRegion(pos)
    local materials, occupancies = terain:ReadVoxels(region, 4)
    local size = materials.Size -- Same as occupancies.Size
    for x = 1, size.X, 1 do
        for y = 1, size.Y, 1 do
            for z = 1, size.Z, 1 do
                print(("(%2i, %2i, %2i): %.2f %s"):format(x, y, z, occupancies[x][y][z], materials[x][y][z].Name))

can you tell me what garbiage is this:
"(%2i, %2i, %2i): %.2f %s"

1 Like

Try making sure the region3’s first value is the forward-left-bottom corner and then the second being the back-top-right corner

A post was merged into an existing topic: Feedback - Bypassing the 30 Character Limit

Practice with region3 doing something else then come back and try it again

im sorry man, i dont have time to waste, i have expirience with programing (javascript, C#, phyton, commodore, apple, microsoft basic) and i always use sipler way. can you please tell me what does this garbige:
"(%2i, %2i, %2i): %.2f %s"
does and can you tell me why i get error:
13:10:36.784 - Region has to be aligned to the grid (use Region3:ExpandToGrid)

grid is expanded to 10 and still this error

1 Like

If you are proficient in those languages like you say you are, you should be capable of recognizing simple string formatting.

1 Like

i dont use that stuff, i mostly programed machines for industrial work
so angles, positions, presure, hegiht, mass values…

Time to learn

Refer to the previously posted links:


Thank you bro but no. Maybe next time, but now i use logic (position Y and other values)
Have good day.

1 Like