I have a Part and I want to align it with another Part using Raycasts. The result I want is identical to how dragged parts align when moving them in Studio.
Here’s my current script:
local RunService = game:GetService("RunService")
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local Part = Instance.new("Part")
Part.Size = Vector3.new(1, 1, 1)
Part.Anchored = true
Part.CanCollide = false
Part.CanQuery = false
Part.Parent = game:GetService("Workspace")
RunService.RenderStepped:Connect(function()
local Result = game:GetService("Workspace"):Raycast(Mouse.Origin.Position, CFrame.lookAt(Mouse.Origin.Position, Mouse.Hit.Position).LookVector * 1000)
if Result then
Part.CFrame = CFrame.lookAt(Result.Position, Result.Position + Result.Normal) * CFrame.new(0, 0, -0.5)
end
end)
This works fine until the surface is rotated on a different axis other than the one the Part is being aligned to. Here’s an example of what I mean:
External Media