Making a part lean towards the mouse

Hi all, how would I make a part lean towards the mouse just like this?

brave_ToKDwCJQnE

1 Like

I am personnally not very good in CFrame, it’s pretty hard. But it is an idea of ​​how you can do this.

--< Services
local UserInputService = game:GetService("UserInputService")

--< Variables
local CameraPart = workspace:WaitForChild("CameraPart")
local Model = workspace:WaitForChild("Model")

local CurrentCamera = workspace.CurrentCamera

local Enabled = false
local NeutralPosition = Vector3.new()

--< Camera
CurrentCamera.CameraType = Enum.CameraType.Scriptable
CurrentCamera.CameraSubject = CameraPart
CurrentCamera.CFrame = CameraPart.CFrame

--< Connections
UserInputService.InputBegan:Connect(function(inputObject)
	if (inputObject.UserInputType == Enum.UserInputType.MouseButton1) then
		NeutralPosition = inputObject.Position
		Enabled = true
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if (inputObject.UserInputType == Enum.UserInputType.MouseButton1) then
		Enabled = false
	end
end)

UserInputService.InputChanged:Connect(function(inputObject)
	if (inputObject.UserInputType == Enum.UserInputType.MouseMovement) and Enabled then
		local displacement = (inputObject.Position - NeutralPosition)
		local currCF = Model:GetPivot()
		Model:PivotTo(currCF * CFrame.Angles(0, math.rad(displacement.X), 0))
	end
end)

I only managed to make it rotate on itself but with a little patience and a few questions to ChatGPT or with the help of other dev better in CFrame you could certainly achieve your goal.

Place1.rbxl (54,8 Ko)