I’m working on an unit system using raycast to make the unit’s Y stay correctly on top of the maps it is at.
The issue is that the raycast bugs and makes so it goes through slopes/some parts.
raycast function:
local function getUnitFloor(unitPosition:Vector3)
local result = workspace:Raycast(unitPosition, Vector3.new(0, -20, 0), raycastParams)
if not result then return end
if not result.Instance then return end
return result, result.Instance
end
what sets the position:
local models = unitFolder.Models:FindFirstChild(if not self.isChampion then 'Default' else 'Champion')
local size : Vector3 = models:GetExtentsSize()
local result, floor = getUnitFloor(Vector3.new(self.Position.X, self.Position.Y + (size.Y/2), self.Position.Z))
if not result then return end
if floor then self.TempValues.Floor = floor end
local unitPosition = Vector3.new(self.Position.X, (result.Position.Y + size.Y/2 * 1.2), self.Position.Z) -- (floor.Size.Y/2 + )
im pretty new to raycasting so im sorry if it looks bad
It’s possible that your raycast is starting very close to the surface of the ground the character is standing on, and sometimes micro fluctuations in the Y-position of the character causes the start point of the raycast to be just under the surface. If a raycast starts inside a part then it will ignore the part!
If your issue is pathfinding check the pathfinding mesh, and make sure your bridges actually have a “good enough” collision fidelity. You can set fidelity in the model properties. If your bridge was made from unions, go to:
Studio Settings > Physics > Show Decomposition Geometry
The physical collision hull the game engine uses to determine collisions can be seen here. With this you can determine whether a raycast at certain positions will fall through objects.
More importantly, regarding your raycasting code, you should add a small buffer like +0.05 or even +0.1 on the y-axis just before raycasting downwards. This is because, if the raycast starts at the exact Y position where the face of the bridge exists, it can clip through sometimes, ignoring the bridge entirely.