Raycast not detecting terrain

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want workspace:raycast to detect the terrain
  2. What is the issue? Include screenshots / videos if possible!
    It wont detect terrain, it only detects parts. It shoots a ray downwards from the sky, but it will only detect the spawn pad or some other test item inside the terrain.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried making the raycastparams collision group the same as the terrains, I have set ignorewater to false. Nothing seems to change it
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

My code is here:

-- declare variables/constants
minLength = -500
maxLength = 500
minWidth = -500
maxWidth = 500
numberOfOres = 1000
oreLocation = game.ReplicatedStorage:WaitForChild("Ores")
caster = game.Workspace:WaitForChild("cast")

--this function generates the ores
function generate()
	for i = 0, numberOfOres do
		local x = math.random(minLength, maxLength)
		local z = math.random(minWidth, maxWidth)
		local params = RaycastParams.new()
		params.FilterType = Enum.RaycastFilterType.Blacklist
		params.FilterDescendantsInstances = {caster}
		params.IgnoreWater = false
		params.CollisionGroup = "Default"
		local cast = workspace:Raycast(Vector3.new(x, 500, z), Vector3.new(x, -400, z) - Vector3.new(x, 500, z), nil)
		if cast then
			print(cast.Position)
			local clone = oreLocation:GetChildren()[math.random(1, #oreLocation:GetChildren())]:Clone()
			clone.Parent = game.Workspace.renderedobjects
			clone.CFrame = CFrame.new(cast.Position)
		end
	end
end
generate()
3 Likes

I even used a YouTube tutorial that was made very recently, it still didn’t work.

1 Like

I don’t understand the purpose of the cast being done here in the first place?

1 Like

It creates a cast that starts really high in the sky, and points downwards. I get the position of the terrain intersection (where the raycast hits), and clone a part to that location. As mentioned earlier, the cast will only recognize BaseParts and not Terrain.

1 Like

You forgot to pass your params to Raycast. You’re passing nil right now.

2 Likes

Even when it wasn’t nil, it did the same thing. Its nil right now because I also tried just removing it outright. That isn’t the issue sadly, any other suggestions?

1 Like

In the second parameter to Raycast you have
Vector3.new(x, -400, z) - Vector3.new(x, 500, z)
You are subtracting out the x and z component, the result of this calculation will always be (0, -900, 0)

1 Like

It does cast down at the terrain, it just doesn’t detect it. If I replaced my terrain with parts it would detect it.

2 Likes

Hello,

Raycasting can detect terrain. You can tell if the ray hits a terrian by checking the RaycastResult.Instance using

:isA"Terrain"

For more information see the RaycastResult documentation

Try replacing this code with:

Vector3.new(0, -900, 0)

So that the math isn’t as confusing as it is.

But that’s the thing, the raycast returns nil most of the time.

I will make sure to do that when this issue is resolved.

Out of curiosity, do you have other collision groups in your game? And if you click on Terrain in the explorer what is its CollisionGroup set to?

If the CollisionGroup in your RaycastParams (or Default if not provided, like in your case) cannot collide with the CollisionGroup of the BasePart/Terrain you want to hit then the ray will pass through. Just wanted to rule this out as a possibility.

I tried it without explicitly setting the ray’s CollisionGroup. The reason I tried to set its CollisionGroup was because that was the terrains CollisionGroup. So sadly that isn’t the issue, thank you though!

Cool, thanks for clearing that up. Another thing to check is that there is terrain between (-500, -900, -500) and (500, 500, 500)?

Here’s a reproduction of your issue using the same code and some random terrain I generated in a new place. Seems to work fine, so I’m guessing we’re missing some information to help you.

Here’s the reproduction place file as well.
OreSpawner.rbxl (64.4 KB)

Only two things I can really think of is that the rays aren’t ever actually hitting terrain or the terrain’s CollisionGroup is being set to something that does not collide with Default.

2 Likes

Ok, I found the problem. The terrain doesn’t load in until the character does it looks like. I added game.Players.LocalPlayer.CharacterAdded:Wait() and it seemed to do the trick. Thank you everyone in this thread for their help! I deeply appreciate it!

Oh this is in a local script? That sounds like a mistake honestly, unless you’re using seeded randomness your ores are going to be in different places for every player in the game.

That is the plan, it is a single player experience after all.

1 Like

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