I'm currently using two rays, one for the right leg and one for the left leg, to detect if an NPC is over an edge. This code is functional. Because of Roblox physics, one leg can be over the edge of a cliff and the NPC still stay on top of the cliff.
The problem is, however, that the way the rest of my code is set up, moving away from the cliff is not instantaneous. Therefore, although the NPC tries to correct itself, it still falls. It's even worse when the Torso is parallel to the cliff edge.
I suppose I could take the coordinates of the brick and detect if the NPC is nearing the edge, but this is messy and I don't want to have to edit my code every time I change the location or size of the brick.
This is not a request, since I'm only asking for a good method, not for code.
Use my raycasting code for reference, but it's really not needed, as my method is flawed.
function script.Parent.OnInvoke(left,right)
--Left leg
local ray = Ray.new(left.Position, Vector3.new(0,-1,0) * 50)
local hitLeft,positionLeft = workspace:FindPartOnRay(ray)
--Right leg
local ray = Ray.new(right.Position, Vector3.new(0,-1,0) * 50)
local hitRight,positionRight = workspace:FindPartOnRay(ray)
if not hitLeft or not hitRight then
return true
end
end

1 comment
Unfortunately that doesn't help the situation in which the NPC is facing sideways to the cliff edge or when its back is toward the cliff.