Part gradually rotating to face a moving part

Hi all,

I was wondering if it was possible to slowly rotate one part to face another. The complication being that the second part is in motion, ruling out the possibility of using tweens or some other form of built-in interpolation.

Here is my current attempt to solve the problem:

while true do
    wait()
    local v1 = workspace.p1.Position
    local currentAngles = Vector3.new(script.Parent.CFrame:ToEulerAnglesXYZ())
    local targetAngles = Vector3.new(CFrame.new(script.Parent.CFrame.p, v1):ToEulerAnglesXYZ())
    local angularDisplacement = targetAngles - currentAngles
        
    script.Parent.CFrame = script.Parent.CFrame * CFrame.fromEulerAnglesXYZ(angularDisplacement.x, angularDisplacement.y, angularDisplacement.z)
end

This causes strange behavior when the part reaches a certain range, I would also maintain that there is likely a more efficient way to do this. A friend suggested that I attempt to normalize the vectors representing the Euler angles but to no avail. Any attempt to help would be appreciated.

7a260675cece91d1b229b2c5344513ba

1 Like

I would use lerping Vector3:Lerp and CFrame:Lerp with small alphas in a loop/renderstep

Thanks for the tip, I didn’t realize it could be simplified to this

1 Like