Mouse rotation stuck

Hello! so I was trying to make a rotatable part that you can spin Left or Right with your mouse. I am having trouble getting the part to spin full 360 and it keeps glitching and wanting to point towards the ground if anyone could possibly help me I would appreciate it!

Problem that occurs
https://gyazo.com/ae1cf86c9e2aaad462aebb6e56c3a998

Code I have now

local mouse = game.Players.LocalPlayer:GetMouse()
local part = workspace.TEST

local click = false

mouse.Button1Up:Connect(function()
	click = false
end)

mouse.Button1Down:Connect(function()
	click = true
end)

mouse.Move:Connect(function()
	if click == true then
		part.CFrame = CFrame.new(part.Position, Vector3.new(mouse.Hit.X, 0,0,0)) else
		if click == false then
			return click
		end
	end
end)
1 Like
local mouse = game.Players.LocalPlayer:GetMouse()
local part = workspace.TEST

local click = false

mouse.Button1Up:Connect(function()
	click = false
end)

mouse.Button1Down:Connect(function()
	click = true
end)

mouse.Move:Connect(function()
    if click then
        part.CFrame = CFrame.lookAt(
            part.Position,
            Vector3.new(
                mouse.Hit.Position.X,
                0,
                mouse.Hit.Position.Z
            )
        )
    end
end)

you could try this out

I have tried something like this, it does in fact fully rotate but when you move the mouse in a circle sometimes it snaps back and fourth and doesn’t really rotate in a perfect circle if that makes sense?
I need it like this https://gyazo.com/4649ee7cf3384c8f93012bd12c544ffa

1 Like

If your trying to achieve that rotation effect your going to need an entire different approach as currently your following the mouse not using it to rotate.

Could you maybe point me in the right direction to go on about this?

Perhaps you should try projecting your part’s position to a world point on screen, and subtracting the distance between the initial world point (from top left corner of screen), and your mouse vector coordinates X and Y. This will return an offset between the world point coordinates and mouse coordinates. Using atan2, you can convert this offset directly to a radian which can be used to rotate the part on an axis. Hope this helps :slight_smile:

local vector, onScreen = camera:WorldToScreenPoint(part.Position)
local screenPoint = Vector2.new(vector.X, vector.Y) - Vector2.new(mouse.X, mouse.Y)
part.CFrame = CFrame.new(part.CFrame.Position) * CFrame.Angles(0, math.atan2(screenPoint.X, screenPoint.Y), 0)
1 Like

Yes! thank you it works this is what I was looking for I never knew you had to use WorldToScreen because I had already tried CFrame angles but it didn’t work.

1 Like