Leg distance downcasting

Okay hopefully I just would like to ask. Is there a way to write this piece of code better?

This is what the code does:

  • Raycast Straight Downwards if Raycast is found then it will check the leg distance
  • If no Raycast straight downwards if found then it will Raycast slightly forwards then check the leg distance
  • It will do that for slightly forwards and slightly backwards etc.
    The reason why I am doing it like this is I do not want to use more Raycast than I have to because there is something else that is more intensive than this going on too.

I have a feeling that a lot of the code is repeated…


while wait(0.1) do
	for i,v in ipairs(FeetTable) do
		local RaycastResult = nil
		local Origin = RP[v.Name]
		local Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(0,-100,0))
		local RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
		if RaycastResult then--Downward
			CheckLegDistance(RaycastResult,v.Name,Origin)
		else
			local Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(0,-100,50))
			local RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
			if RaycastResult then--Forward
				CheckLegDistance(RaycastResult,v.Name,Origin)
			else
				local Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(0,-100,-50))
				local RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
				if RaycastResult then--Backward
					CheckLegDistance(RaycastResult,v.Name,Origin)
				else
					local Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(50,-100,0))
					local RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
					if RaycastResult then--Left
						CheckLegDistance(RaycastResult,v.Name,Origin)
					else
						local Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(-50,-100,0))
						local RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
						if RaycastResult then--Right
							CheckLegDistance(RaycastResult,v.Name,Origin)
						else
							warn("NO RAYCAST FOUND")
						end
					end
				end
			end
		end
	end
end

If there is a special function or type of loop that does this please let me know. Thanks for reading.

So I have a couple of alternative ways to write this just to make it cleaner. I prefer the top one better than the bottom one because I think it is easier to tell at a glance what is going on. I made the use of continue which basically says “go to the next loop and skip everything else”. It’s basically the same as your code, but it doesn’t have a bunch of indentations.

while wait(0.1) do
	for i,v in ipairs(FeetTable) do
		local Origin = RP[v.Name]

		local Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(0,-100,0))
		local RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
		if RaycastResult then CheckLegDistance(RaycastResult,v.Name,Origin) continue end

		Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(0,-100,50))
		RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
		if RaycastResult then CheckLegDistance(RaycastResult,v.Name,Origin) continue end

		Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(0,-100,-50))
		RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
		if RaycastResult then CheckLegDistance(RaycastResult,v.Name,Origin) continue end

		Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(50,-100,0))
		RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
		if RaycastResult then CheckLegDistance(RaycastResult,v.Name,Origin) continue end

		Direction = Origin.WorldCFrame:VectorToWorldSpace(Vector3.new(-50,-100,0))
		RaycastResult = workspace:Raycast(Origin.WorldPosition,Direction,RaycastParameters)
		if RaycastResult then CheckLegDistance(RaycastResult,v.Name,Origin) continue end

		warn("NO RAYCAST FOUND")
	end
end

This is my second try, which uses a loop. It’s even shorter and even easier to add or remove directions, but I don’t like that you need to look at it for a few seconds to realize what it does.

local Directions = {Vector3.new(0,-100,0), Vector3.new(0,-100,50), Vector3.new(0,-100,-50), Vector3.new(50,-100,0), Vector3.new(-50,-100,0)}
while wait(0.1) do
	for i,v in ipairs(FeetTable) do
		local Origin = RP[v.Name]
		local Found = false -- Only for the warning
		for _, Direction in ipairs(Directions) do
			local RaycastResult = workspace:Raycast(Origin.WorldPosition,Origin.WorldCFrame:VectorToWorldSpace(Direction),RaycastParameters)
			if RaycastResult then
				CheckLegDistance(RaycastResult,v.Name,Origin)
				Found = true -- Only for the warning
				break
			end
		end
		if not Found then warn("NO RAYCAST FOUND") end
	end
end

I really like the look of this. Thanks a bunch for your help. It looks a lot better now!

1 Like