What do you want to achieve?
I’ve been trying to make a raft/boat that goes at full speed only when it’s in water, and slows down significantly when it’s not
What is the issue?
When printing the raycast result, it always prints nil, no matter what solution I’ve tried. The raycast always finds nothing, no matter what’s below it.
What solutions have you tried so far?
I looked through many solutions of the DevForum, but the ones that make sense don’t seem to work. I tried increasing the direction length from -1 to -100, I tried bringing the raycast origin height to a higher position, etc.
Example of something I tried:
My code:
local rayParams = RaycastParams.new(workspace.Terrain)
rayParams.FilterType = Enum.RaycastFilterType.Include
rayParams.IgnoreWater = false
local function waterMult()
local result = workspace:Raycast(Vector3.new(primary.Position)+Vector3.new(0,10,0), Vector3.new(0,-1,0)*100, rayParams)
print(result) -- always printing nil
if result then
if result.Material == Enum.Material.Water then
return 1 -- full speed
else
return .1 -- diminished speed
end
end
return .1 -- no result found (diminished speed)
end
What is primary? RaycastParams.new doesn’t recieve a parameter, so I’m not sure what you were trying to do. You also shouldn’t using Enum.RaycastFilterType.Include.
Code (assuming that primary is just a part in the boat, and that primary.Parent is the boat’s model):
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude
local function waterMult()
rayParams.FilterDescendantsInstances = {primary.Parent}
local result = workspace:Raycast(primary.Position + Vector3.new(0, 10, 0), Vector3.new(0, -100, 0), rayParams)
if result and result.Material == Enum.Material.Water then
return 1 -- full speed
end
return .1 -- no result found (diminished speed)
end
My bad, I found a solution to my problem that doesn’t require raycasting at all. It works beautifully.
Thank you Katrist for helping me, you rewrote my code much more efficiently than I did
The code:
local function waterMult()
local primePos = primary.Position
local min = primePos - primary.Size
local max = primePos + primary.Size
local region = Region3.new(min,max):ExpandToGrid(4)
local material = workspace.Terrain:ReadVoxels(region,4)[1][1][1]
if material == Enum.Material.Water then
return 1 -- full speed
end
return .1 -- water not found (diminished speed)
end