Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why won't my 3rd person mouse-lock camera script work?

Asked by
cfiredog 274 Moderation Voter
8 years ago
Edited 8 years ago

I have made a script which should allow players to move the camera around with their mouse while in third person:

Cam = workspace.CurrentCamera
Cam.CameraType = "Scriptable"
Character = game.Players.LocalPlayer.Character
Humanoid = Character:WaitForChild("Humanoid")
UserInputService = game:GetService("UserInputService")


UserInputService.InputChanged:connect(function(inputObject)

    if Humanoid then
    UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter    --Locks Mouse to center of screen

    if inputObject.UserInputType == Enum.UserInputType.MouseMovement then    --If Mouse moves then:

    game:GetService("RunService"):BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, function()
            Cam.CFrame = CFrame.new(Character.HumanoidRootPart.Position, Vector3.new(inputObject.Delta.X, inputObject.Delta.Y, 0))  --This is the part that is probably wrong
            end)

        end

    end

end)

THE PROBLEM:

The problem that I am having is updating the look vector of the camera so that a player can move his/her mouse to look around (line 16).

MY KNOWLEDGE OF THE SITUATION:

I know that the reason this code isn't working right now is because the look vector of the camera is using only the Delta.X and Delta.Y inputs (which change values when the mouse is moving, but go back to 0 once the mouse stops moving). Because I'm only using these inputs, the values will reset back to 0 once the mouse stops moving, and so the look vector will ultimately remain fixed at the center of the game workplace.

MY QUESTION:

How should I go about updating the look vector so that it will not reset to 0 and remain fixed at the center of the game workplace? I believe I have to add the Vector3 (Delta.X, Delta.Y, 0) to something, but I am not sure what exactly to add it to if this is the case.

Any help on this would be greatly appreciated!

Edit: Fixed some formatting issues.

1 answer

Log in to vote
0
Answered by 8 years ago

In settings you can put it in mouse-lock.

0
That's not what I need help with. I already have that done. cfiredog 274 — 8y
0
Okay, but can't say I didn't try to help. lonestarmusic 0 — 8y
Ad

Answer this question