So I’m trying to make a script that lets you grab onto ledges and climb stuff. First it detects the closest part in front of the player then sends a bunch of rays diagonally in front of the player to detect where the ledge is.
I got 2 issues:
- When I try to draw the ray with a part it ends in a place different to that of the intersection of the ray (the line is the ray, the dots are the intersections)
- For some reason there’s intersections on TOP of the part I’m sending raycasts to.
I don’t know if I’m doing something wrong or it’s just raycast errors. Any ideas?
ContextActionService:BindAction("ShootRay",
function(ActionName, InputState, InputObject)
if InputState == Enum.UserInputState.End then
if rig.IsGrabbingLedge == false then
-- Grab onto ledge
-- Check distance
rig.IsGrabbingLedge = true
local origin = rig.Root.Position
local destination = rig.Root.CFrame.LookVector*300
local distanceResult = workspace:Raycast(origin,destination,ledgeRayParams)
local distanceFromPlayer;
print(distanceResult == nil)
VisualizeRay(origin, destination,distanceResult, Color3.fromRGB(255, 255, 255), 500)
if distanceResult then
-- If the ray hits, check if it's distance is small enough for the player to grab to climb.
distanceFromPlayer = (distanceResult.Position - rig.Root.Position).Magnitude
print(distanceFromPlayer)
local rayClosestToLedge;
local part;
if distanceFromPlayer < ledgeDistance then
-- numeric loop to create many rays that will intersect the part, until no more rays can be made and the last one is used.
for i = 1, 20, 1 do
local origin1 = rig.Root.Position
local destination1 = distanceResult.Position + (Yvector*i)
local result1 = workspace:Raycast(origin1, destination1, ledgeRayParams)
if result1 then
print("Instance is "..result1.Instance.Name)
part = VisualizeVector(destination1, Color3.fromRGB(255, 255, 255), 100)
rayClosestToLedge = result1
elseif not result1 then
break
end
end
part.Color = Color3.fromRGB(55, 255, 0)
if rayClosestToLedge then
VisualizeRay(rig.Root.Position, nil, rayClosestToLedge, Color3.fromRGB(0, 255, 0), 100)
else
end
end
end
else
rig.IsGrabbingLedge = false
print("IS grabbing ledge")
-- Vault
end
end
end,
false, Enum.KeyCode.R )
This is what happens. The white ray is the one used to check the distance and the green ray is supposed to be the one closest to the ledge. All the white dots are ray intersections