I haven’t messed around with this, but I assume just set it to false and then you could detect if the ray doesn’t hit terrain water then you can stop the force.
the last argument, IgnoreWater (which is a bool value) can be used to make sure the ray isnt hitting water, since that’s not what you are looking for.
So you could cast a ray going down from the bottom of the boat, and listen for what it hits. So heres a script example:
local objecthit, hitposition = workspace:FindPartOnRay(YourRayHere, YourRaftHere, false, true) -- last argument is true to make sure we arent listening for water
if objecthit.Name == "Terrain" or objecthit.ClassName == "Part" then
if (YourRayHere.Origin - hitposition).magnitude < 2 then -- checking if the terrain is close enough to the raft's hull
--stop your raft
end
end
Heres another way to make your ray have a limit to how far it can detect parts, to make sure you aren’t just stopping when your ray hits the bottom of the lake, which is clearly not water.
I wasn’t giving you a script, I was giving you a snippet of a larger script. My purpose in responding to you wasn’t just to give you a script, because it won’t help anyone to learn. I was trying to show you a Method from which you can implement it into your own code.
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
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 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:
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.
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
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.
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.
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?