Getting points on terrain

I’m messing around with random numbers for placement of things on terrain, and I was wondering: Is there any way that I can give a script the terrain (which is not flat) and be able to select a random point on that terrain?

2 Likes

Try and give this a read Environmental Terrain | Documentation - Roblox Creator Hub

Should be a part about generating terrain

2 Likes

I read through it and couldn’t find anything. I’ll rephrase what I’m going for:


A script to be able to just select a random point on the terrain like the black dots shown. I’m doing this for animal spawning in the game.

2 Likes

You could just raycast from random positions, and find the intersection of the ray.

2 Likes

Could you please show me an example? I’m not very advanced with raycasting.

2 Likes

Alright, here’s an example:

for i = 1, 10 do
	local x = math.random(-100, 100)
	local z = math.random(-100, 100)
	
	local raycastResult = workspace:Raycast(Vector3.new(x, 500, z), Vector3.new(0, -1000, 0))
	print(raycastResult.Position)
end

You can optimize this way further if you know how tall the terrain can be and how short it will be, so tweak the Y values as you wish.

1 Like

Thank you so much! So if I understand this correctly, a raycast is a line that goes from one Vector3 position to another, and it returns the position where it first intersected an object?

1 Like

Yes, but that second argument I’m specifying is not another position, but instead a direction vector.

Yes, the raycastResult also contains quite a few other useful details.

2 Likes

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