Glitchy rotation

Hey everyone,

I’ve been trying to use trig to make an object look at a certain position. Here’s a video of my attempt:

Here’s the code for it:

local part = workspace.Part; 
local follow = workspace.follow; 

follow.Changed:Connect(function()
	local follow_from_part = part.CFrame:ToObjectSpace(follow.CFrame)
	-- lol SOH (sin -> opp/hyp)
    local hypotenuse = math.abs(follow_from_part.Position.Magnitude)
	local opposite = math.abs(follow_from_part.X)
	part.CFrame *= CFrame.Angles(0, -math.asin(opposite/hypotenuse), 0)
end)

As you can see, it does these weird glitches when it rotates sometimes. I’m not sure what I did wrong so if you know, please leave a reply down below. Thanks!

PS: note that I’m not the best at math; might not understand what you are saying :slight_smile:

2 Likes

You need to save the original CFrame of the part and use that as a foundation for the rest of the angles or itll just keep spinning

For example:

local origCF = part.CFrame

follow.Changed:Connect(function()
    --trig stuff

    part.CFrame = origCF * CFrame.Angles(0, angle, 0)
end)

What its doing now is going:
Add 90 to 0 degrees
Add 90 to 90 degrees
Add 90 to 180 degrees

Add 90 to 320895230785372059238 degrees

It just keeps adding upon itself and spinning out of control

1 Like

oh that makes sense. thanks for your help; i’ll try doing this!

EDIT: it works. thanks man

1 Like