Changing object rotation using mousemovement

hello, I’m making an object where you can click and rotate whatever you want with your mouse, but I have a problem with my object rotation, I don’t want to make the object face to the mouse, but what I want to make is that the object changes their Rotation using mouse.Move without facing the mouse. for example


game from Bad Business btw

My attempt


and here’s the code

mouse.Button1Down:Connect(function()
	hold = true 
	game:GetService("RunService").RenderStepped:Wait()
end)
mouse.Button1Up:Connect(function()
	hold = false
end)
mouse.Move:Connect(function()
	if hold == true then
		model.PrimaryPart.CFrame = CFrame.new(model.PrimaryPart.Position, Vector3.new(mouse.Hit.X, mouse.Hit.Y, mouse.Hit.p.Z))
	end
end)

even though I use mouse.Hit without the position but still, the object always face to the mouse

4 Likes

I would probably use UserInputService and Mouse.Move for this.

local model = workspace.Model

local dx, dy = 0, 0 -- in degrees
local MouseSensitivity = 0.5

local UserInputService = game:GetService("UserInputService")
local Mouse = game.Players.LocalPlayer:GetMouse()

function UpdateModel()
	model.PrimaryPart.CFrame = CFrame.new(model.PrimaryPart.Position)
		* CFrame.fromOrientation(math.rad(dx), math.rad(dy), 0)
end

function MouseMove()
	local Delta = UserInputService:GetMouseDelta()
	dx += Delta.Y * MouseSensitivity
	dy += Delta.X * MouseSensitivity

	UpdateModel()
end

function MouseDown()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
end

function MouseUp()
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default
end

Mouse.Move:Connect(MouseMove)
Mouse.Button1Down:Connect(MouseDown)
Mouse.Button1Up:Connect(MouseUp)

Extra: if you want to limit how much you can rotate the model, then use math.clamp(number, min, max).

Edit: Remember to set the Camera’s CameraType to “Scriptable”.

11 Likes

Oh thank god bro, it works perfectly fine. thank you for your help, appreciated!

is there a way to make the model return to it’s original position when mouse is released?

1 Like

You could always just set the dx and dy variables to 0.

1 Like