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.
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.