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