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.