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

How to make scriptable camera not snap when toggling between scriptable and custom?

Asked by 4 years ago

So I'm working on a scripted camera system for 1st person. The gist is it's toggleable. You can turn it off and on but my thing is that when I turn it off facing a direction, and turn it back on, no matter where I faced my camera, it snaps back to it's original cframe https://gyazo.com/aa63a3fa2a30716163e914b04e228ee0 It's really annoying and I want it to update so it's not snapping it just keeps going seamlessly.

repeat wait() until game.Players.LocalPlayer.Character
--// Variables
local player = game.Players.LocalPlayer
local char = player.Character
local valueFolder = char:WaitForChild('CarbonValues')
local mouse = player:GetMouse()

local storage = game.ReplicatedStorage:WaitForChild('CarbonStorage')
local eventFolder = storage:WaitForChild('Events')
local moduleFolder = storage:WaitForChild('Modules')
local weaponFolder = storage:WaitForChild('Weapons')
local weapClient = weaponFolder:WaitForChild('Client')
local weapServer = weaponFolder:WaitForChild('Server')

local humanoid = char:WaitForChild("Humanoid")
local rootPart = char:WaitForChild("HumanoidRootPart")

local switched = false;
local mode = true;
local enabled = true;

--// Tables
local control = {
    cam = workspace.CurrentCamera;
    camOffset = Vector3.new(0, 0, 0);
    cameraAngleX = 0;
    cameraAngleY = 0;

    xDelta = 0;
    yDelta = 0;

    mouseY = valueFolder:WaitForChild('MouseY').Value;
}

local modules = {
    ragDollMod = require(moduleFolder:WaitForChild('Ragdoll'));
    springMod = require(moduleFolder:WaitForChild('Spring'));
    utilitiesMod = require(moduleFolder:WaitForChild('Utilities')); 
}

--// Services
local ContextActionService = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local ts = game:GetService('TweenService')

--// Functions
function playerInput(inputObject)
    control.cameraAngleX = control.cameraAngleX - inputObject.Delta.X
    -- Reduce vertical mouse/touch sensitivity and clamp vertical axis
    control.cameraAngleY = math.clamp(control.cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
    -- Rotate root part CFrame by X delta
end

function focusControl()
    uis.MouseIconEnabled = false

    if mode then
        humanoid.AutoRotate = false
        --control.camOffset = Vector3.new(2, 2, 5)
    else
        humanoid.AutoRotate = true
        --control.camOffset = Vector3.new(0, 2, 5)
    end
end

--// Inputs
uis.InputBegan:connect(function(input,chat)
    if not chat then
        if input.UserInputType == Enum.UserInputType.MouseButton2 then
            --mode = true
            --focusControl()
        end; 

        if input.KeyCode == Enum.KeyCode.F then
            enabled = not enabled

            if not enabled then
                control.cam.CameraType = Enum.CameraType.Custom
                control.cam.CameraSubject = humanoid
            end
        end

        if input.KeyCode == Enum.KeyCode.V then
            switched = not switched

            if not switched then
                control.camOffset = Vector3.new(2, 2, 5)
            else
                control.camOffset = Vector3.new(-2, 2, 5)
            end;
        end;
    end
end)

uis.InputEnded:Connect(function(input,chat)
    if not chat then
        if input.UserInputType == Enum.UserInputType.MouseButton2 then
            mode = false
            focusControl()
        end;
    end;
end)

uis.InputChanged:connect(function(input,chat)
    if not chat then
        if input.UserInputType == Enum.UserInputType.MouseMovement then
            playerInput(input)
        end;
    end
end)

--// Init
focusControl()

--// Renders
RunService.RenderStepped:Connect(function(dt)
    if enabled then
        if control.cam.CameraType ~= Enum.CameraType.Scriptable then
            control.cam.CameraType = Enum.CameraType.Scriptable
        end

        uis.MouseBehavior = Enum.MouseBehavior.LockCenter
        local startCFrame = CFrame.new((char:WaitForChild('Head').CFrame.Position)) * CFrame.Angles(0, math.rad(control.cameraAngleX), 0) * CFrame.Angles(math.rad(control.cameraAngleY), 0, 0)
        local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(control.camOffset.X, control.camOffset.Y, control.camOffset.Z))
        local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(control.camOffset.X, control.camOffset.Y, -10000))
        control.cam.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)

        local newNeck = char:WaitForChild('Torso'):WaitForChild('Neck')
        local HRPCF =  char:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 1.5, 0) * CFrame.new(char:WaitForChild("Humanoid").CameraOffset)
        newNeck.C0 = newNeck.C0:lerp(char:WaitForChild("HumanoidRootPart").CFrame:toObjectSpace(HRPCF) * CFrame.Angles(math.asin((mouse.Hit.p - mouse.Origin.p).unit.y),0,0),0.2)
        newNeck.C1 = newNeck.C1:lerp(CFrame.new(), 0.2)

        --[[for _,v in pairs(char:GetChildren()) do
            if v and v:IsA('BasePart') then
                v.LocalTransparencyModifier = 1
            elseif v and v:IsA('Accessory') then
                v:WaitForChild('Handle').LocalTransparencyModifier = 1
            end
        end]]--

        if not mode then
            rootPart.CFrame = rootPart.CFrame:lerp(CFrame.new(rootPart.CFrame.p, rootPart.CFrame.p + control.cam.CFrame.lookVector * Vector3.new(0.1, 0, 0.1)), 1)
        end
    end;
end)

Answer this question