I’m making a ledge grab mechanic in my game, I’ve already got the raycast set up and have anchored the player. My only issue is that I’m not sure how I’m supposed to calculate the face that a raycast hit of raycast.Instance
, so that I can then offset the position of the player correctly that corresponds to the correct orientation of the part.
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local root = char.PrimaryPart
local origion = root.Position
local direction = root.CFrame.LookVector * 1.5
local vizual = Instance.new("Part", workspace)
vizual.Anchored = true
vizual.CFrame = root.CFrame
vizual.CanCollide = false
vizual.Name = "Visualizer"
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true
params.FilterDescendantsInstances = {char, vizual}
game:GetService("RunService").RenderStepped:Connect(function()
local midpoint = origion + direction/2
origion = root.CFrame.Position direction = root.CFrame.LookVector * 1.5
vizual.Size = Vector3.new(1, 1, direction.Magnitude)
vizual.CFrame = CFrame.new(midpoint, origion)
local ray = workspace:Raycast(origion, direction, params) if ray then
if ray.Instance ~= nil and ray.Instance.Name == "ledgePart" and root.Position.Y < ray.Instance.Position.Y then
root.Anchored = true
root.CFrame = workspace.ledgePart.CFrame - Vector3.new(0, 2, -2) -- This only works for the back of the part.
end
end
end)