Log in to vote

How to make player rotate relative to camera while having tool equipped and holding RMB? [closed]

Asked by 8 months ago

I already have it so that the player can rotate relative to their movement whilst the cursor is locked to the center screen. Here's the code for that in case it is needed.

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = workspace.CurrentCamera
local cameraOffset =, 2, 5)
local player = Players.LocalPlayer


    local humanoid = character:WaitForChild("Humanoid")
    local rootPart = character:WaitForChild("HumanoidRootPart")
    humanoid.AutoRotate = true

    local cameraAngleX = 0
    local cameraAngleY = 0

    local function playerInput(actionName, inputState, inputObject)
        -- Calculate camera/player rotation on input change
        if inputState == Enum.UserInputState.Change then
            cameraAngleX = cameraAngleX - inputObject.Delta.X
            -- Reduce vertical mouse/touch sensitivity and clamp vertical axis
            cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
            -- Rotate root part CFrame by X delta

    ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)

        if camera.CameraType ~= Enum.CameraType.Scriptable then
            camera.CameraType = Enum.CameraType.Scriptable
        local startCFrame = * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
        local cameraCFrame = startCFrame:ToWorldSpace(, cameraOffset.Y, cameraOffset.Z))
        local cameraFocus = startCFrame:ToWorldSpace(, cameraOffset.Y, -10000))
        camera.CFrame =, cameraFocus.Position)

local function focusControl(actionName, inputState, inputObject)
    -- Lock and hide mouse icon on input began
    if inputState == Enum.UserInputState.Begin then
        UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
        UserInputService.MouseIconEnabled = true
        ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

Closed as Not Constructive by Leamir

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?