Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Getting angle between mouse and part?

Asked by 6 years ago
local hinge = workspace.hingepart.HingeConstraint
local p = hinge.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function()
    local pos = p.Position
    local angleRad = math.asin((pos - mouse.Hit.p).unit.X)
    local angleDeg = math.deg(angleRad)
    hinge.TargetAngle = angleDeg
end)

That script is supposed to get the angle between the position of a part and the position of my mouse, and set that angle to the targetangle of a hingeconstraint so that the part attached to the hinge "points" toward my mouse.

It works, but only half way. Check out this gif to see what I mean more clearly:

https://gyazo.com/281d39d833a9a6a8256488b033bffacb

As you can see, it works fine when my mouse is below the part, but when I move it above, it is inverted.

I've tried also using the dot product to get the angle between two vectors by using the steps outlined in this article, but I'm getting the same issue:

https://medium.com/@EgoMoose/the-scary-thing-known-as-the-dot-product-5124be3a66aa

Does anyone know how to get the angle between two vectors without this problem? Is there something I'm missing? Any help will be appreciated.

0
Why don't you use a BodyGyro for this? Seems like it would fit. chomboghai 2044 — 6y
0
I would still need to find the correct angle, and so that I can have it welded to another model and keep the rotating motion via the hinge. dunkmaster452 1 — 6y
0
Nevermind, I'm dumb. Thanks for your suggestion, BodyGyro worked by settings the CFrame of it to CFrame.new(pos, mouse.Hit.p) dunkmaster452 1 — 6y

Answer this question