So Im trying to make my own lookat kinda thing and im taking the arc cosine of the dot product to get the angle in radians but its just not working as intended
local p2 = script.Parent
local p1 = workspace.p1
game:GetService("RunService").Heartbeat:Connect(function()
local position = (p1.Position - p2.Position).Unit
local look = p2.CFrame.LookVector.Unit
local result = position:Dot(look)
p2.CFrame = CFrame.new(p2.Position) * CFrame.Angles(0,math.acos(result),0)
end)
The parts are both positioned in a way where the angle converted into degrees should returned should be zero for the Y Rotation but the dotproduct returns .99 which when I use the acos function on it and then convert it to degrees it doesnt give me exactly zero unless I use math.ceil on the dot product and then use the acos function
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).