You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want workspace:raycast to detect the terrain
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.
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()
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.
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?
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)
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.
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.