Aim offset part at target point while only modifying the first part's rotation

  1. What do you want to achieve?
    image
    Essentially, I want to rotate part A so that the ray from part B will intersect with part C.

  2. 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.
    image
    However, when I move part C up on the Y axis, it begins to lose accuracy.
    image
    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.
    image

  3. 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

I believe I have done this already in my module with the one mptor dummy example, may I ask what the purpose of yours is though?

1 Like

I’m making a gun for my game but I want the firing to be realistic instead of something like this so I was making a script to turn the character’s arm, but welds got too complex so I made a mock-up with parts
image

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.