Raycasting to detect terrain?

Ik,I did but it is not working.I was checking it out in smaller scale like if it works it would print but nothing happens thats why I was asking if there is something I could fix

2 Likes

Could you make a simple test place and post it? I’d be happy to help you figure it out.

1 Like

…your part’s position wouldn’t happen to be 9 studs above the surface of the baseplate, would it?

That would be the conditions required to print absolutely nothing in the output log.

1 Like
local objecthit, hitposition,normal,material = workspace:FindPartOnRayWithWhitelist(YourRayHere, {workspace.Terrain}, false, false)
local hitwater = false
if objecthit.Name == "Terrain" and material  == Enum.Material.Water then
hitwater = true
end
--if hitvalue is false, cancel out velocity in that corners direction. Otherwise, do nothing.

I believe this might work better.

1 Like

I even tried 100 but still it does not work

1 Like

How are you constructing your rays?

1 Like

https://gyazo.com/39462187ab898a908eb39d99142c658e --Script

Also UnCopylocked Place : https://www.roblox.com/games/2767277027/RaftProblem

1 Like

I tested the place. First things first, the code is in a local script so it doesn’t run. You need to place it in a server script if you want it to run server-side.

Also I’ve found out the issue. The problem is that the position of the raft is already in the water, so it ignores the water and the next thing the ray picks up is the grass underneath. Rays have this weird thing where if the starting position is within a part, it ignores that part. To fix this is simple, set the starting position of the ray to be a little bit higher than the raft position. I changed the first line to this and it worked:

YourRayHere = Ray.new(workspace.Raft.Position + Vector3.new(0, 10, 0), Vector3.new(0,-10,0)*100)
4 Likes

You might also wanna have a ray being cast from a position in the front and one in the back so that if you accidently hit land you’re still able to go back.

3 Likes

So I edited the script and it is working fine but the problem is that now how can I repeat the script efficiently every time the raft moves to check the raft is not on Land

–script

YourRayHere = Ray.new(workspace.Raft.Model.Position + Vector3.new(0, 10, 0), Vector3.new(0,-10,0)*100)

YourRaftHere = script.Parent

local objecthit, hitposition = workspace:FindPartOnRay(YourRayHere, YourRaftHere, false, false)

if objecthit.Name == "Terrain" or objecthit.ClassName == "Part" then

if (YourRayHere.Origin - hitposition).magnitude < 10 then – checking if the terrain is close enough to the raft’s hull

print("Working")–stop raft

end

end

1 Like

You can put it in a while loop and check like every 0.5 seconds, like this:

while wait(0.5) do
-- Put all the code here
end
1 Like

I will try it :slight_smile:

2 Likes

Bad idea. I would use a RunService.Stepped loop if done on the server, or a RunService.Heartbeat loop on the client. This is not an expensive call, one ray a frame is nothing. If you have some sort of event that fires when the ship moves, use that though.

2 Likes

How is it a bad idea? I’ve done checks like this countless of times and I never had to do it every frame. The ship doesn’t move fast enough that you need to check every frame. If anything, the wait delay could be dropped lower to like 0.1 if you want more precision, but definitely not every frame.

1 Like

You can do around 60,000 raycast calls without any performance issues. There’s no reason to do it in a fixed amount of time as slow as 0.5 seconds, There can be visible time delays which makes it look horrible. Why would you not do it every frame?

1 Like

Well there’s really no reason to do it every frame because the boat doesn’t move at lightning speed. Thats why I said, you could drop it down to 0.1 seconds or just use wait() which will wait around 0.03 seconds. Thats enough to not see any visible difference at all. Running the code even faster only takes up extra performance with practically no improvement.

1 Like

That’s every single frame, precisely what I am saying to use.

I stated why to do this

1 Like

Thats only if the player is running at 30 FPS (which is honestly quite low). And yes I realize what you’re saying about the delay issue but I don’t think it will be visible if ran every 0.1 seconds. I agree 0.5 seconds may be a bit too slow, but I don’t think it should be any lower than 0.1

Either way, its up to @IllustriousEntity how often to run the loop.

2 Likes

Doesn’t matter. Please remain on-topic. These optimisation practices can be discussed with OP privately or you can raise a discussion about this by creating a new thread, not hijacking a thread about detecting terrain from a raycast.

4 Likes

I just don’t understand your reasoning for being lower than wait()… As I said, there’s little to no drawbacks with a huge plus, instant feedback.

2 Likes