How to Detect Terrain Material

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

This should help you answer this answer

(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

I think it already ignores air lol.

1 Like

Nope. I tried printing the material and it said air. :tired_face:

1 Like

I’ll try this:
local _, _, _, material = workspace:FindPartOnRayWithWhitelist(ray,{Enum.Material.Water})

2 Likes

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

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