WARNING: THIS INVOLVES COMPLEX MATH THAT I DON'T UNDERSTAND.
So I'm trying to make an AI Car. I found some code on how to point a hinge at a brick, but it appears it only works on the Z axis. When I place the car on the X Axis, the car just turns around in circles. How do I fix this?
Devforum post where I found the code (modified): https://devforum.roblox.com/t/how-do-i-make-a-hingeconstraints-targetangle-face-my-character/406041/17
AI Code:
while wait() do
-- define position of origin
HKpos = Vector2.new(script.Parent.Parent.Parent.PrimaryPart.NonPhysics.ROOT.Position.X,script.Parent.Parent.Parent.PrimaryPart.NonPhysics.ROOT.Position.Z)
-- define position of target
XYpos = Vector2.new(game.Workspace.wAypoint.Position.X,game.Workspace.wAypoint.Position.Z)
-- the ratio to get the triangle's width over the height
ratio = (XYpos.Y - HKpos.Y) / (XYpos.X - HKpos.X)
beta = math.atan(ratio) -- converting this to radians
-- turn it from radians into degrees
beta = beta*(180/math.pi)
print(beta)
script.Parent.HingeConstraint.TargetAngle = -beta -- beta has to be negative so it points at you
script.Parent.Parent.Parent.FR.Mount.HingeConstraint.TargetAngle = -beta
end
