Ledge Detection Problem when orientation isn't (0,0,0)

  1. What do I want to achieve
    What i want to achieve is getting the ledge of the part even if its rotation isn’t (0,0,0)

  2. The issue
    When the orientation of the part isn’t (0,0,0) or rotated
    the part Y position is no longer at the top and made the ledge inaccurate.

Here is some images to make things more clear

Red is what the player supposed to hold on
Blue is what the player actually hold on

when orientation is (0,0,0)
When orientation 0
when orientation is (-90,0,0)
when orientation is at 90

  1. What solutions have I tried so far?
    i tried looked around youtube and i tried using CFrame (the one that i made isn’t reliable and im pretty certain i did something wrong with the CFrame but idk what is the problem)

i have looked around dev forums and google and i can’t seem to find the solution to this either.

-- Part of the code--
local origin = Head.CFrame.Position
	local direction = Head.CFrame.LookVector*3
	local ray = workspace:Raycast(origin, direction, RayParams)
	if ray and canledgeon and not Hanging then
		local RayPart = ray.Instance
		if RayPart.Size.Y >= 5 then
			if Head.Position.Y >= (RayPart.Position.Y + (RayPart.CFrame.Position.Y/2)) - 1 --(RayPart.Size.Y / 2)) - 1 
				and Head.Position.Y <= RayPart.Position.Y + (RayPart.CFrame.Position.Y/2) --(RayPart.Size.Y/2) 
				and Hum.FloorMaterial == Enum.Material.Air 
				and HRP.Velocity.Y <= 0 then                              
				HRP.Anchored = true 
				Hanging = true
				canledgeon = false
				print(Player.Name.."is Hanging")

it would be great if you guys can give me some insights and ideas on how to solve this problem

Thanks in advance