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

How do I make an artificial Mouse Lock?

Asked by 10 years ago

Ok so I'm trying to figure out how to make an artificial mouse lock where your camera is viewing you from 3rd person, but you can control the camera by moving the mouse as if you were using the MouseLock feature of Roblox. I'm not asking for code, all I'm asking is for suggestions as to how I would go about making this. Any suggestions?

2 answers

Log in to vote
0
Answered by
woodengop 1134 Moderation Voter
10 years ago

I have answered this Question Before

                                                                                                                                                                                                                wait(1)
ALLOW_TOGGLE = true    -- true/false Allows the shoulder camera to be toggled using the key binded below.
TOGGLE_KEY = "p"       -- If ALLOW_TOGGLE is true, this key is used to toggle the shoulder camera. Also you can change it to right click if you you want to, or change it to other hot keys.
DEFAULT_ENABLED = true -- true/false. The default enabled state of the shoulder camera. Forced to true if toggling isn't allowed.
SMOOTH_ROTATION = true -- true/false. Makes your torso turn smoothly instead of sharply. Note that it puts a Body Gyro into the Torso.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

player = game.Players.LocalPlayer
mouse = player:GetMouse()
enabled = DEFAULT_ENABLED
c = workspace.CurrentCamera
mirror = {}
h = nil
userInput = game:GetService("UserInputService")
isMobile = false

function createModel()
    local m = Instance.new("Model",c)
    m.Name = "ViewModel"
    local hack = Instance.new("Part",m)
    hack.Name = "HumanoidRootPart"
    hack.Transparency = 1
    hack.CanCollide = false
    hack.Anchored = true
    hack.CFrame = CFrame.new()
    local h = Instance.new("Humanoid",m)
    h.Sit = true
    h.Changed:connect(function ()
        h.Sit = true
    end)
    return m
end

function registerPart(v)
    local function mirrorPart(p,t)
        local m = p:clone()
        m.CanCollide = false
        m.Parent = model
        m.Transparency = (t or 0)
        local w = Instance.new("Weld",m)
        w.Part0 = p
        w.Part1 = m
        mirror[v] = m
        if m.Name == "Head" then
            m.Name = "H"
        end
        v.Changed:connect(function (property)
            local blist = {Position = true, CFrame = true, Parent = true, CanCollide = true}
            if not blist[property] then
                pcall(function () -- Some properties can't be queried :P
                    m[property] = v[property]
                end)
            end
        end)
        v.ChildAdded:connect(function (ch)
            ch.Archivable = true
            local cl = ch:clone()
            cl.Parent = m
            mirror[ch] = cl
        end)
        v.ChildRemoved:connect(function (ch)
            if mirror[ch] then
                mirror[ch]:Destroy()
                mirror[ch] = nil
            end
        end)
        m.Changed:connect(function ()
            m.CanCollide = false
        end)
    end
    if v:IsA("BasePart") and not mirror[v] and v.Name ~= "HumanoidRootPart" then
        mirrorPart(v)
    elseif v:IsA("Hat") then
        local h = v:WaitForChild("Handle")
        mirrorPart(h)
    elseif v:IsA("Clothing") or v:IsA("CharacterAppearance") then
        v:clone().Parent = model
    end
end

function updateCam()
    if h then
        if enabled then
            if model then
                local range = model:GetExtentsSize().Y/2
                local y = c.CoordinateFrame.lookVector.Y
                h.CameraOffset = Vector3.new(math.max(1.5,1.5-y),math.abs(y/2)-y*3.8642337322235,4-math.abs(y))
            else
                h.CameraOffset = Vector3.new(1.5,0.5,4)
            end
        else
            h.CameraOffset = Vector3.new()
        end
    end
end

function newCharacter(char)
    if not char then return end
    mirror = {}
    for _,v in pairs(c:GetChildren()) do
        if v.Name == "ViewModel" then
            v:Destroy()
        end
    end
    if not enabled then return end
    model = createModel()
    for _,o in pairs(char:GetChildren()) do
        registerPart(o)
    end
    char.ChildAdded:connect(registerPart)
    h = nil
    while not h do
        -- Why am I doing it like this? Well, in case you're me and you're re-naming 
        -- your humanoids, its nicer to not change the name of everything.
        for _,v in pairs(char:GetChildren()) do
            if v:IsA("Humanoid") then
                h = v
                break
            end
        end
        wait()
    end
    updateCam()
    player.CameraMaxZoomDistance = 0.5
    h.Died:connect(function ()
        if model then
            model:Destroy()
        end
    end)
    local t = char:WaitForChild("Torso")
    for _,v in pairs(t:GetChildren()) do
        if v.Name == "ShoulderCamGyro" then
            v:Destroy()
        end
    end
    if SMOOTH_ROTATION and enabled and mouse then
        local dir = c.CoordinateFrame.lookVector * Vector3.new(1,0,1)
        local b = Instance.new("BodyGyro",t)
        b.Name = "ShoulderCamGyro"
        b.maxTorque = Vector3.new(999999,999999,999999)
        b.cframe = CFrame.new(t.Position,t.Position+(c.CoordinateFrame.lookVector*Vector3.new(1,0,1)))
        b.D = 100
        game:GetService("RunService").RenderStepped:connect(function ()
            if h then
                h.AutoRotate = not enabled
            end
            if b and t then
                local dir = c.CoordinateFrame.lookVector * Vector3.new(1,0,1)
                b.cframe = CFrame.new(t.Position,t.Position+dir)
            end
        end)
    end
end

if mouse and ALLOW_TOGGLE and #TOGGLE_KEY == 1 and type(TOGGLE_KEY) == "string" then
    mouse.KeyDown:connect(function (key)
        if string.lower(key) == TOGGLE_KEY then
            enabled = not enabled
            if model then
                model:Destroy()
                newCharacter(player.Character)
                updateCam()
            end
        end
    end)
    mouse.Move:connect(updateCam)
end

if player.Character then
    newCharacter(player.Character)
end

local con do
    con = game:GetService("RunService").RenderStepped:connect(function ()
        isMobile = userInput.TouchEnabled
        if isMobile then
            con:disconnect()
            SMOOTH_ROTATION = false -- You can also Switch this to TRUE
            newCharacter(player.Character)
        end
    end)
end

Make sure you Use a LocalScript and Put it in StarterGui or StarterPack.

0
Thanks! Really helped! TurboFusion 1821 — 10y
0
No Problem. woodengop 1134 — 10y
0
:O I CAN'T BELIEVED I HAD HELPED TURBOFUSION! woodengop 1134 — 9y
Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

Use the AutoRotate property of Humanoid to prevent the character from turning automatically. Then use the Move event of Mouse to detect when the mouse moves, and rotate the character, perhaps with CFrame?

Answer this question