Player Mouse Manipulation

Manipulates your camera by moving your mouse!

by ARadioactivePizza

Author Avatar

This will show you how to make your camera manipulate whenever your mouse moves.

First, we need to detect when our mouse moves.

local uis = game:GetService('UserInputService')

uis.MouseBehavior = Enum.MouseBehavior.LockCenter

uis.InputBegan:Connect(function(inputobject)
    if inputobject.UserInputType == Enum.UserInputType.MouseMovement then
    print('Moved!')
end
end)

Ok, we detected when the mouse moves. When the mouse moves, it will print, "Moved!". Now, you're probably wondering, "Well, wheres the mouse camera manipulation thingymajigy?" Ok, well look, we havent gotten into that yet. Reminder this this is a HARD TUTORIAL. You may need to understand the concept of CFrame and game.Player.Character and something like that. If we wanted the manipulation, well you're gonna have to delete the mouse movement script.

Ok, so, we want to do the same beginning as the mouse movement script. Oh yeah, also capitalize some of the phrases too.

local UserInputService = game:GetService('UserInputService')

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false

What's happening here is that were doing the same thing as the mouse movement detecting script except we're disabling the mouse's icon.

Next, we want to classify the camera.

local UserInputService = game:GetService('UserInputService')

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false

local Camera = game.Workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable

local Angle = 0

Now what's happening here is that, we're making a variable for the Workspace's camera. In this case it's CurrentCamera. Then, we're changing the camera's type to Scriptable. This means that you can script the camera's "settings" and change it's statistics. The "Angle" variable is the angle is the camera's magnitude, or should I say, distance.

Might I say, we could be copying some of the parts from the MMD script.

local UserInputService = game:GetService('UserInputService')

UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false

local Camera = game.Workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable

local Angle = 0

UserInputService.InputChanged:Connect(function(inputobject)
    if inputobject.UserInputType == Enum.UserInputType.MouseMovement then
        if inputobject.Delta.x < 0 then
            Angle = Angle - 5
        elseif inputobject.Delta.x > 0 then
            Angle = Angle + 5
        end
    end 
end)

game:GetService('RunService').RenderStepped:Connect(function()
    local x = 10 * math.cos(math.rad(Angle))
    local z = 10 * math.sin(math.rad(Angle))

    local PositionCamera = Vector3.new(x, 5 ,z)
    local PlayerTorso = game.Players.LocalPlayer.Character.UpperTorso.Position -- Change 'UpperTorso' to 'Torso' if you want this to be R6, or leave it be if you want it to be R15

    PositionCamera = PositionCamera + PlayerTorso
    Camera.CFrame = CFrame.new(PositionCamera, PlayerTorso)
end)

Ok, I know this might be hard to understand, but I will explain it.

We're detecting the mouse's movement so that we can change the angle's value. If the "inputobject" delta is lesser that 0, then it will shorten down the distance. It the delta is greater than 0, it will increase the distance. Now, we're going into the RunService. RenderStepped is a pretty hard one to explain, so for the sake of this tutorial, I'm not explaining it. So, we're setting values for the X and Z corrdinates of Vector3.new. In this case, were using math.cos, math.sin, and math.rad. Then, we're classifying the camera's position using the corrdinates we just set values for. Then, we're getting the character's torso (our UpperTorso. You can change this to just "Torso" if you are using R6.). Then, were puttion the camera's position to our torso, with the "Angle" value all set up.

Phew, that was a toughie. If you dont wan't to come here everytime just to CTRL+C and CTRL+V my script, I made it as a free model just for you! Also, put the script in StarterGui. Much obliged!

View in-game to comment, award, and more!