-
What do you want to achieve?
Essentially, I want to rotate part A so that the ray from part B will intersect with part C. -
What is the issue? For the most part, the script I wrote works flawlessly if I move part C around on the X and Z axes.
However, when I move part C up on the Y axis, it begins to lose accuracy.
Also, if I adjust part B so that it is not offset at a perfect 90 degree angle from part A, it screws up the accuracy as well.
-
What solutions have you tried so far? There were no solutions I could find on the devforum, and I tried rewriting the script multiple times. This is the script I have so far, which is 100% original.
local a = script.Parent.A
local b = script.Parent.B
local c = script.Parent.C
local o = script.Parent.O
local weldAB = a.CFrame:Inverse() * b.CFrame
local AB = (b.Position - a.Position).Magnitude
local angleB = math.acos((b.Position - o.Position).Unit:Dot(b.CFrame.LookVector))
-- law of sines
game:GetService("RunService").Stepped:Connect(function()
local aCFrame = CFrame.lookAt(o.Position, c.Position)
local bCFrame = aCFrame * weldAB
local axis = aCFrame.UpVector
local AC = (c.Position - o.Position).Magnitude
local angleC = math.asin(AB / (AC / math.sin(angleB)))
local angleA = math.pi - angleB - angleC
local angleA_Original = math.acos((bCFrame.Position - o.Position).Unit:Dot((c.Position - o.Position).Unit))
a.CFrame = CFrame.lookAt(o.Position, c.Position) * CFrame.fromAxisAngle(axis, angleA_Original - angleA)
end)
I suspect the problem has to do with calculating the angle, or the CFrame.fromAxisAngle
constructor being inherently flawed, which it probably isn’t.
I have also provided a model for people who want to experiment in studio - pointer thingy - Roblox