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

How Do I Change The Shoulder Camera Script In The "Auto Rifle" Model By Roblox Continuation 2?

Asked by 3 years ago
Edited 3 years ago

Check my original post "How Do I Change The Shoulder Camera Scripts In The "Auto Rifle" Model By Roblox To Change Plr Camera?" and check my second post "How Do I Change The Shoulder Camera Scripts In The "Auto Rifle" Model By Roblox Continuation?"

--Continuation 4
function ShoulderCamera:onCurrentCharacterChanged(character)
    self.currentCharacter = character
    if self.currentCharacter then
        self.raycastIgnoreList[1] = self.currentCharacter
        self.currentHumanoid = character:WaitForChild("Humanoid")
        self.currentRootPart = character:WaitForChild("HumanoidRootPart")

        self.rootRigAttach = self.currentRootPart:WaitForChild("RootRigAttachment")
        self.rootJoint = character:WaitForChild("LowerTorso"):WaitForChild("Root")
        self.currentWaist = character:WaitForChild("UpperTorso"):WaitForChild("Waist")
        self.currentWrist = character:WaitForChild("RightHand"):WaitForChild("RightWrist")
        self.wristAttach0 = character:WaitForChild("RightLowerArm"):WaitForChild("RightWristRigAttachment")
        self.wristAttach1 = character:WaitForChild("RightHand"):WaitForChild("RightWristRigAttachment")
        self.rightGripAttachment = character:WaitForChild("RightHand"):WaitForChild("RightGripAttachment")

        self.currentTool = character:FindFirstChildOfClass("Tool")

        self.eventConnections.humanoidDied = self.currentHumanoid.Died:Connect(function()
            self.zoomedFromInput = false
            self:updateZoomState()
        end)
        self.eventConnections.characterChildAdded = character.ChildAdded:Connect(function(child)
            if child:IsA("Tool") then
                self.currentTool = child
                self:updateZoomState()
            end
        end)
        self.eventConnections.characterChildRemoved = character.ChildRemoved:Connect(function(child)
            if child:IsA("Tool") and self.currentTool == child then
                self.currentTool = character:FindFirstChildOfClass("Tool")
                self:updateZoomState()
            end
        end)

        if Players.LocalPlayer then
            local PlayerScripts = Players.LocalPlayer:FindFirstChild("PlayerScripts")
            if PlayerScripts then
                local PlayerModule = PlayerScripts:FindFirstChild("PlayerModule")
                if PlayerModule then
                    self.controlModule = require(PlayerModule:FindFirstChild("ControlModule"))
                end
            end
        end
    else
        if self.eventConnections.humanoidDied then
            self.eventConnections.humanoidDied:Disconnect()
            self.eventConnections.humanoidDied = nil
        end
        if self.eventConnections.characterChildAdded then
            self.eventConnections.characterChildAdded:Disconnect()
            self.eventConnections.characterChildAdded = nil
        end
        if self.eventConnections.characterChildRemoved then
            self.eventConnections.characterChildRemoved:Disconnect()
            self.eventConnections.characterChildRemoved = nil
        end

        self.currentTool = nil
        self.currentHumanoid = nil
        self.currentRootPart = nil
        self.controlModule = nil
    end
end

function ShoulderCamera:onCurrentCameraChanged(camera)
    if self.currentCamera == camera then
        return
    end

    self.currentCamera = camera

    if self.currentCamera then
        self.raycastIgnoreList[2] = self.currentCamera

        if self.eventConnections.cameraTypeChanged then
            self.eventConnections.cameraTypeChanged:Disconnect()
            self.eventConnections.cameraTypeChanged = nil
        end
        self.eventConnections.cameraTypeChanged = self.currentCamera:GetPropertyChangedSignal("CameraType"):Connect(function()
            if self.enabled then
                self.currentCamera.CameraType = Enum.CameraType.Scriptable
            end
        end)
    end
end

function ShoulderCamera:isHumanoidControllable()
    if not self.currentHumanoid then
        return false
    end
    local humanoidState = self.currentHumanoid:GetState()
    return CONTROLLABLE_HUMANOID_STATES[humanoidState] == true
end

function ShoulderCamera:getCollisionRadius()
    if not self.currentCamera then
        return 0
    end
    local viewportSize = self.currentCamera.ViewportSize
    local aspectRatio = viewportSize.X / viewportSize.Y
    local fovRads = math.rad(self.fieldOfView)
    local imageHeight = math.tan(fovRads) * math.abs(self.currentCamera.NearPlaneZ)
    local imageWidth = imageHeight * aspectRatio

    local cornerPos = Vector3.new(imageWidth, imageHeight, self.currentCamera.NearPlaneZ)
    return cornerPos.Magnitude
end

function ShoulderCamera:penetrateCast(ray, ignoreList)
    local tries = 0
    local hitPart, hitPoint, hitNormal, hitMaterial = nil, ray.Origin + ray.Direction, Vector3.new(0, 1, 0), Enum.Material.Air
    while tries < 50 do
        tries = tries + 1
        hitPart, hitPoint, hitNormal, hitMaterial = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList, false, true)
        if hitPart and not hitPart.CanCollide then
            table.insert(ignoreList, hitPart)
        else
            break
        end
    end
    return hitPart, hitPoint, hitNormal, hitMaterial
end

function ShoulderCamera:getRelativePitch()
    if self.currentRootPart then
        local pitchRotation = CFrame.Angles(self.pitch, 0, 0)
        local relativeRotation = self.currentRootPart.CFrame:toObjectSpace(pitchRotation)
        local relativeLook = relativeRotation.lookVector

        local angle = math.asin(relativeLook.Y)
        return math.clamp(angle, self.minPitch, self.maxPitch)
    end
    return self.pitch
end

function ShoulderCamera:getCurrentFieldOfView()
    if self.zoomState then
        return self.zoomedFOV
    else
        return self.fieldOfView
    end
end

function ShoulderCamera:handlePartTransparencies()
    local partsLookup = {}
    local accoutrementsLookup = {}

    for _, child in pairs(self.currentCharacter:GetChildren()) do
        local hidden = false
        if child:IsA("BasePart") then
            hidden = partsLookup[child.Name] == true
            child.LocalTransparencyModifier = hidden and 1 or 0
        elseif child:IsA("Accoutrement") then
            local descendants = child:GetDescendants()
            local accoutrementParts = {}
            for _, desc in pairs(descendants) do
                if desc:IsA("Attachment") and accoutrementsLookup[desc.Name] then
                    hidden = true
                elseif desc:IsA("BasePart") then
                    table.insert(accoutrementParts, desc)
                end
            end
            for _, part in pairs(accoutrementParts) do
                part.LocalTransparencyModifier = hidden and 1 or 0
            end
        elseif child:IsA("Tool") then
            hidden = self.zoomState and (self.hasScope or self.hideToolWhileZoomed)
            for _, part in pairs(child:GetDescendants()) do
                if part:IsA("BasePart") then
                    part.LocalTransparencyModifier = hidden and 1 or 0
                end
            end
        end
    end
end

1 answer

Log in to vote
0
Answered by 3 years ago
--Continuation 5
function ShoulderCamera:setSprintEnabled(enabled)
    self.sprintEnabled = enabled
end

function ShoulderCamera:setSlowZoomWalkEnabled(enabled)
    self.slowZoomWalkEnabled = enabled
end

function ShoulderCamera:setHasScope(hasScope)
    if self.hasScope == hasScope then
        return
    end

    self.hasScope = hasScope
    self:updateZoomState()
end

function ShoulderCamera:onSprintAction(actionName, inputState, inputObj)
    self.sprintingInputActivated = inputState == Enum.UserInputState.Begin
end


-- Zoom related functions

function ShoulderCamera:isZoomed()
    return self.zoomState
end

function ShoulderCamera:setHideToolWhileZoomed(hide)
    self.hideToolWhileZoomed = hide
end

function ShoulderCamera:setZoomFactor(zoomFactor)
    self.currentZoomFactor = zoomFactor
    local nominalFOVRadians = math.rad(self.fieldOfView)
    local nominalImageHeight = math.tan(nominalFOVRadians / 2)
    local zoomedImageHeight = nominalImageHeight / self.currentZoomFactor
    self.zoomedFOV = math.deg(math.atan(zoomedImageHeight) * 2)
    self:updateZoomState()
end

function ShoulderCamera:resetZoomFactor()
    self:setZoomFactor(self.defaultZoomFactor)
end

function ShoulderCamera:setForceZoomed(zoomed)
    if self.forcedZoomed == zoomed then return end
    self.forcedZoomed = zoomed
    self:updateZoomState()
end

function ShoulderCamera:setZoomedFromInput(zoomedFromInput)
    if self.zoomedFromInput == zoomedFromInput or (self.currentHumanoid and self.currentHumanoid:GetState() == Enum.HumanoidStateType.Dead) then
        return
    end

    self.zoomedFromInput = zoomedFromInput
    self:updateZoomState()
end

function ShoulderCamera:updateZoomState()
    local isZoomed = self.forcedZoomed
    if self.canZoom and not self.forcedZoomed then
        isZoomed = self.zoomedFromInput
    end

    if not self.enabled or not self.currentTool then
        isZoomed = false
    end

    self.zoomState = isZoomed

    self.currentMouseRadsPerPixel = isZoomed and self.zoomedMouseRadsPerPixel or self.mouseRadsPerPixel
    self.currentTouchSensitivity = isZoomed and self.zoomedTouchSensitivity or self.touchSensitivity

    if self.weaponsSystem and self.weaponsSystem.gui then
        self.weaponsSystem.gui:setCrosshairScaleTarget(self.zoomState and self.zoomedCrosshairScale or self.normalCrosshairScale)
        self.weaponsSystem.gui:setCrosshairEnabled(not self.zoomState or not self.hasScope)
        self.weaponsSystem.gui:setScopeEnabled(self.zoomState and self.hasScope)
        if self.currentTool then
            self.currentTool.ManualActivationOnly = self.zoomState and self.hasScope and UserInputService.TouchEnabled
        end
    end

    if self.currentCamera then
        self.desiredFieldOfView = self:getCurrentFieldOfView()
    end
end

function ShoulderCamera:onZoomAction(actionName, inputState, inputObj)
    if not self.enabled or not self.canZoom or not self.currentCamera or not self.currentCharacter or not self.weaponsSystem.currentWeapon then
        self:setZoomedFromInput(false)
        return Enum.ContextActionResult.Pass
    end

    self:setZoomedFromInput(inputState == Enum.UserInputState.Begin)
    return Enum.ContextActionResult.Sink
end


-- Recoil related functions

function ShoulderCamera:setCurrentRecoilIntensity(x, y)
    self.currentRecoil = Vector2.new(x, y)
end

function ShoulderCamera:addRecoil(recoilAmount)
    self.currentRecoil = self.currentRecoil + recoilAmount
end


-- Input related functions

function ShoulderCamera:applyInput(yaw, pitch)
    local yInvertValue = UserGameSettings:GetCameraYInvertValue()
    self.yaw = self.yaw + yaw
    self.pitch = math.clamp(self.pitch + pitch * yInvertValue, self.minPitch, self.maxPitch)
end

function ShoulderCamera:processGamepadInput(dt)
    local gamepadPan = self.gamepadPan
    if gamepadPan then
        gamepadPan = gamepadLinearToCurve(gamepadPan)
        if gamepadPan.X == 0 and gamepadPan.Y == 0 then
            self.lastThumbstickTime = nil
            if self.lastThumbstickPos.X == 0 and self.lastThumbstickPos.Y == 0 then
                self.currentGamepadSpeed = 0
            end
        end

        local finalConstant = 0
        local currentTime = tick()

        if self.lastThumbstickTime then
            local elapsed = (currentTime - self.lastThumbstickTime) * 10
            self.currentGamepadSpeed = self.currentGamepadSpeed + (6 * ((elapsed ^ 2) / 0.7))

            if self.currentGamepadSpeed > 6 then self.currentGamepadSpeed = 6 end

            if self.lastGamepadVelocity then
                local velocity = (gamepadPan - self.lastThumbstickPos) / (currentTime - self.lastThumbstickTime)
                local velocityDeltaMag = (velocity - self.lastGamepadVelocity).Magnitude

                if velocityDeltaMag > 12 then
                    self.currentGamepadSpeed = self.currentGamepadSpeed * (20 / velocityDeltaMag)
                    if self.currentGamepadSpeed > 6 then
                        self.currentGamepadSpeed = 6
                    end
                end
            end

            finalConstant = GameSettings.GamepadCameraSensitivity * self.currentGamepadSpeed * dt
            self.lastGamepadVelocity = (gamepadPan - self.lastThumbstickPos) / (currentTime - self.lastThumbstickTime)
        end
        self.lastThumbstickPos = gamepadPan
        self.lastThumbstickTime = currentTime

        local yawInput = -gamepadPan.X * finalConstant * self.gamepadSensitivityModifier.X
        local pitchInput = finalConstant * gamepadPan.Y * GameSettings:GetCameraYInvertValue() * self.gamepadSensitivityModifier.Y

        self:applyInput(yawInput, pitchInput)
    end
end


0
check my post "How Do I Change The Shoulder Camera Script In The "Auto Rifle" Model By Roblox Continuation 3?" for the rest of the code MrSarp_shoot 37 — 3y
Ad

Answer this question