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

In need of help with my OTS/3rd person camera script, trying to get the mouse to move screen?

Asked by 5 years ago

Yesterday I posted a question which didn't include any code. [It got moderated] So I took Donut's advice and made my script. In this LocalScript I instantiate a part, weld it to the HumanoidRootPart of the character and offset it to where I want the camera location to be; over the right shoulder.

local cameraPart = Instance.new("Part")
cameraPart.CanCollide = false
cameraPart.Size = Vector3.new(1,1,1)
cameraPart.FrontSurface = Enum.SurfaceType.Hinge
cameraPart.Massless = true
cameraPart.Name = ("CameraPart")
local weldPiece = Instance.new("Weld")
weldPiece.Name = ("CameraWeld")
--Functions
local function weldPartToPlayer(weld,part)
    part.Parent = char
    weld.Part0 = hrp
    weld.Part1 = part
    weld.C0 = CFrame.new(offset)*CFrame.Angles(rad(0),rad(0),rad(0))
    weld.Parent = hrp
end
weldPartToPlayer(weldPiece,cameraPart)

Then I utilized RunService to make the camera CFrame become the part's CFrame.

Runservice.RenderStepped:Connect(function()
    camera.CFrame = cameraPart.CFrame
end)

This works perfectly, but it's not finished. Which leads to my question!

How would I lock the mouse to the center and make Delta.X rotate the HumanoidRootPart left to right?

Thank you very much for reading and any help is appreciated! I'll upvote and accept the answer that works best. c:

1
Perfect, thanks for fixing the question. DinozCreates 1070 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

alright so you need to get the deltax first, so you need to use the service UserInputService like so

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local sensitivity = 0.4 -- make variable for sensitivity
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter -- lock the mouse to center
local xAngle = 0 -- make variable for deltax
UserInputService.InputChanged:Connect(function(input,gameEvent)
    if gameEvent == false and input.UserInputType == Enum.UserInputType.MouseMovement then -- making sure you're not moving over a GUI
        xAngle = xAngle - input.Delta.x * sensitivity -- subtract the xAngle by delta.x to inverse it then multiply by sensitivity
    end
end)

then you'd want to make the HumanoidRootPart's Y angle be xAngle cause it's the opposite

Runservice.RenderStepped:Connect(function()
    camera.CFrame = cameraPart.CFrame
    hrp.CFrame = CFrame.new(hrp.Position)*CFrame.Angles(0,math.rad(xAngle),0)
end)
1
It works! Thanks so much! It's a little laggy but I think it's due to the Y axis not being able to move darkhenry 93 — 5y
Ad

Answer this question