How to make player rotate relative to camera while having tool equipped and holding RMB? [closed]
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.
01 | local Players = game:GetService( "Players" ) |
02 | local ContextActionService = game:GetService( "ContextActionService" ) |
03 | local UserInputService = game:GetService( "UserInputService" ) |
04 | local RunService = game:GetService( "RunService" ) |
06 | local camera = workspace.CurrentCamera |
07 | local cameraOffset = Vector 3. new( 2 , 2 , 5 ) |
08 | local player = Players.LocalPlayer |
10 | player.CharacterAdded:Connect( function (character) |
12 | local humanoid = character:WaitForChild( "Humanoid" ) |
13 | local rootPart = character:WaitForChild( "HumanoidRootPart" ) |
14 | humanoid.AutoRotate = true |
16 | local cameraAngleX = 0 |
17 | local cameraAngleY = 0 |
19 | local function playerInput(actionName, inputState, inputObject) |
21 | if inputState = = Enum.UserInputState.Change then |
22 | cameraAngleX = cameraAngleX - inputObject.Delta.X |
24 | cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y* 0.4 , - 75 , 75 ) |
29 | ContextActionService:BindAction( "PlayerInput" , playerInput, false , Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch) |
31 | RunService.RenderStepped:Connect( function () |
32 | if camera.CameraType ~ = Enum.CameraType.Scriptable then |
33 | camera.CameraType = Enum.CameraType.Scriptable |
35 | local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles( 0 , math.rad(cameraAngleX), 0 ) * CFrame.Angles(math.rad(cameraAngleY), 0 , 0 ) |
36 | local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z)) |
37 | local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, - 10000 )) |
38 | camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position) |
42 | local function focusControl(actionName, inputState, inputObject) |
44 | if inputState = = Enum.UserInputState.Begin then |
45 | UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter |
46 | UserInputService.MouseIconEnabled = true |
47 | ContextActionService:UnbindAction( "FocusControl" , focusControl, false , Enum.UserInputType.MouseButton 1 , Enum.UserInputType.Touch, Enum.UserInputType.Focus) |
50 | ContextActionService:BindAction( "FocusControl" , focusControl, false , Enum.UserInputType.MouseButton 1 , Enum.UserInputType.Touch, Enum.UserInputType.Focus) |