How to rotate a part according mouse movement

Hello Developers, today I’m trying to make a part rotate according to mouse movement, like phantom forces. I have made a script but it isn’t working.


As you can see it doesn’t rotate according the mouse movement. It will sometimes rotate in a circular motion. Here’s the script:

local UserInputService = game:GetService("UserInputService")
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local camera = workspace.CurrentCamera
local Rotate = false
local oldLocation = Vector3.new(0,0,0)

camera.CameraType = Enum.CameraType.Scriptable
wait(0.1)
camera.CFrame = workspace.StoreCamera.CFrame
UserInputService.InputChanged:Connect(function (inputObject, gameProcessedEvent)
	local inputPosition = inputObject.Position
	-- ViewportPointToRay would probably be more accurate to use here
	
	if Rotate == true then
		print(math.rad((oldLocation.y - inputPosition.Y) / 250))
		workspace.Handle.CFrame *= CFrame.Angles(math.rad((oldLocation.y - inputPosition.Y) / 250), math.rad((oldLocation.y - inputPosition.Y) / 250), math.rad(((oldLocation.y - inputPosition.Y) / 250) + (oldLocation.X - inputPosition.X) / 250))
	end
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		Rotate = true
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		Rotate = false
		oldLocation = input.Position
	end
end)

I’m not that good with CFrame. I hope someone can solve this problem.

Bumping because I still need help!

You can set Sensitivity to 250 if you want to

The problem was because of this part

The Z of CFrame.Angles is not required so set it to 0

local UserInputService = game:GetService("UserInputService")
local Mouse = game:GetService("Players").LocalPlayer:GetMouse()
local camera = workspace.CurrentCamera
local Rotate = false
local LastMousePosition = Vector2.new(Mouse.X, Mouse.Y)

local Sensitivity = 1

camera.CameraType = Enum.CameraType.Scriptable
task.wait(0.1)
camera.CFrame = workspace.StoreCamera.CFrame

UserInputService.InputChanged:Connect(function(inputObject, gameProcessedEvent)
	if Rotate == true then
		local YOrientation = (Mouse.X - LastMousePosition.X) / Sensitivity
		local XOrientation = (Mouse.Y - LastMousePosition.Y) / Sensitivity

		workspace.Handle:PivotTo(
			workspace.Handle:GetPivot() * CFrame.Angles(math.rad(XOrientation), math.rad(YOrientation), 0)
		)
	end
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		Rotate = true
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		Rotate = false
		LastMousePosition = Vector2.new(Mouse.X, Mouse.Y)
	end
end)
1 Like

Hey, sorry for the late response. I wasn’t at home when I received this. It works great! 1 question though, why does it twitch when I want to rotate it? Here’s what I mean.

The problem was because the part’s cframe angle reset everytime you stop pressing MouseButton2

But here is the solution ( not tested )

3 Likes

I’m really sorry again that I didn’t answer for 3 hours, but it worked! thanks for the help, I’ll mark this as a solution.

1 Like