Here’s a solution using dot product, although I don’t recommend
local p2 = script.Parent
local p1 = workspace.p1
game:GetService("RunService").Heartbeat:Connect(function()
local position = ((p1.Position - p2.Position) * Vector3.new(1, 0, 1)).Unit
local look = (p2.CFrame.LookVector * Vector3.new(1, 0, 1)).Unit
local result = position:Dot(look)
local dir = (position - p2.CFrame.RightVector).magnitude <= (position + p2.CFrame.RightVector).magnitude and -1 or 1
if result < 1 then
p2.CFrame *= CFrame.Angles(0,math.acos(result) * dir,0)
end
end)
Why wouldnt you recommend the dot product solution and also it works great but could you explain everything you changed in detail so I can grasp a better understanding of what i actually did wrong
I think by changing the way you calculate the dot itself, kojo multiplies it by a vector3 before calculating the unit vector, I’m not that great with dot product so it’s best that they explain it lool.
I did this so that the vectors are on a X and Z plane (so its basically 2d vector). Since you’ll only be rotating around the Y axis I made it this way.
As you know, using dot product you could get the angle between. However it doesn’t tell you which direction (ex: dot product of 45° right vs 45° left is both ). I used a hacky way of determining the direction by comparing which side vector is closest to point toward the target.
(vector A is closer to pointing towards the object than vector B; therefore it should turn right).