How to calculate final position with cross normal?

Hello, Developers!
I meet a trouble, when i’m tryed to calculate position of hit point ray, to the cross point one of normals of the hit object. There is image. As you can see, final position is not calculated RIGHT. Blue - hit position, Purple - desired position. Here is my code:

local function GetWorldSize(part, right_vector)
local cf = CFrame.new(part.Position, part.Position - right_vector)
local size = cf:VectorToWorldSpace(part.Size)
return Vector3.new(math.abs(size.X), math.abs(size.Y), math.abs(size.Z))
end

local to,from = workspace:WaitForChild(“To”).Position, workspace:WaitForChild(“From”).Position
local diff = (to-from)
local direction, dist = diff.Unit, diff.Magnitude
local ray = Ray.new(from, direction*dist)

local part,pos,normal = workspace:FindPartOnRay(ray,workspace.From)
dist = (from-pos).Magnitude

local rayPart = Instance.new(“Part”)
rayPart.Transparency = 0.5
rayPart.Color = Color3.new(1, 0, 0)
rayPart.Size = Vector3.new(0.1, 0.1, dist)
rayPart.CFrame = CFrame.new(from, pos)*CFrame.new(0, 0, -dist/2)
rayPart.Anchored = true
rayPart.Parent = workspace

print(direction)

if part then
local up_vector = normal:Cross(direction)
local right_vector = up_vector:Cross(normal)
local objectRightVector = Vector3.new(math.sign(right_vector.X), math.sign(right_vector.Y), math.sign(right_vector.Z))
local full_size = GetWorldSize(part, objectRightVector)

local rayPart = Instance.new("Part")
local side = part.CFrame.Position + (objectRightVector * full_size.X/2)
local dist = (side-pos).Magnitude

print("full dist:", dist)

local pTest = Instance.new("Part")
pTest.Size = Vector3.new(1,1,1)
pTest.Transparency = 0.5
pTest.Anchored = true
pTest.CanCollide = false
pTest.Position = side
pTest.Name = "ImHere!"
pTest.Parent = workspace

rayPart.Transparency = 0.5
rayPart.Color = Color3.new(1, 0.666667, 1)
rayPart.Size = Vector3.new(0.1, 0.1, dist)
rayPart.CFrame = CFrame.new(pos, pos + right_vector)*CFrame.new(0, 0, -dist/2)
rayPart.Anchored = true
rayPart.Parent = workspace

end

Could you please explain why you’re doing this? That would help with figuring out a solution.

Because i’m wanna to do NPC get around the obstacles, if they’re meet of course.

To try and get what I think you tried to show in the image, you would want to reflect the red line along the plane defined by the wall and then you would save that reflected vector as a variable, say “reflectionDirection”. Then, you would simply offset the reflected vector by the point of contact of the surface and then you would get that faces normal and offset it in the opposite direction, orthogonal to it in a sense by the thickness of whatever side you are on. I suppose you could do that by representing a bunch of the normal vectors of the part in the world space and then comparing them to the normal vector the red line hit to see which one is closest to it.

Have you seen the PathfindingService before? Just checking.

Also, not sure if its exactly what you’re looking for, but you can do something like this to find the “exit point” of a raycast:

i don’t need a pathfindingService at this case

yes i could try, but this isn’t the best way to do this