So I’ve never worked with terrain before, but I was commissioned to make a fishing system that works with terrain. When the rod is cast, I need to detect if it is facing water. I could use a part and put it under water, but I rather learn how to do this.
TLDR: How do I detect terrain materials?
2 Likes
Crictoli
(cric)
June 5, 2021, 12:49pm
#2
This should help you answer this answer
The material of the Terrain object is technically undefined because it is just a facilitator object for working with terrain. When it comes to terrain, you actually need to work in respect to its voxels or a raycast that finds the material of the item it hit from the third return value.
What I don’t quite understand is why it doesn’t allow you to just downcast. Try casting directly down again at the part’s position and instead of the DownVector (negative UpVector), just a hard-coded amount of s…
(Short answer)
Basically what he said is that the material of the terrain object is technically undefined so the only thing you can do is raycast it below the part.
local part = ... -- path to rod
local ray = Ray.new(part.Position, Vector3.new(0, -10, 0))
local _, _, _, material = workspace:FindPartOnRayWithWhitelist(ray, {workspace.Terrain})
-- part, position, ignorewater
print(material)
5 Likes
Hey, thanks! I probably won’t use the exact script, but I’ll mess around with this stuff! <3
1 Like
No problem, and it’s fine if you won’t use the exact script.
2 Likes
Quick question tho! What’s with the “, ,_,”. I assume they’re values I don’t need, but what are they?
1 Like
Well, I have already left a comment for you about that, I’ll send it again.
-- part, position, ignorewater
Yes those are the values you don’t need.
1 Like
Oh! xD. Didn’t notice it. Mb. Thanks tho!
2 Likes
And I assume if I want it to NOT ignore water I leave it blank?
1 Like
Even if you changed the value or leave it blank, it would still have the same results, by default it would be false.
(It’s just an unused variable)
However, if you want it to ignore water, you can add the third parameter to true. It would look like:
local _, _, _, material = workspace:FindPartOnRayWithWhitelist(ray, {workspace.Terrain}, true))
2 Likes
I know I have a lot of questions here, I promise I’m a good scripter I’ve just never used terrain before. xD
Anyways, how do I make it ignore Air?
1 Like
Crictoli
(cric)
June 5, 2021, 2:18pm
#11
I think it already ignores air lol.
1 Like
Nope. I tried printing the material and it said air.
1 Like
I’ll try this:
local _, _, _, material = workspace:FindPartOnRayWithWhitelist(ray,{Enum.Material.Water})
2 Likes
Crictoli
(cric)
June 5, 2021, 2:23pm
#14
Yeah, try that. Maybe that’ll work.
1 Like
Yikes. ‘Unable to Cast Value to Object’. I wish we could have ignore lists and whitelists.
1 Like
Crictoli
(cric)
June 5, 2021, 2:26pm
#16
The only thing I can think of is to keep raycasting and adding parts you hit to the ignore list until the next raycast hits another material.
Definitely easy so you can script that. (Not sure if that would work)
1 Like
Yeh. I can try that. o7 Thanks!
2 Likes