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

How can I incorporate a bobble effect with a third person shooter style camera?

Asked by 3 years ago

I made a third person shooter styled camera script and I am trying to also get a camera bobble effect to work along with it, but when I use the scripted third person camera with the camera bobble effect it does not work. If I use only the camera bobble effect with the normal non scripted camera it does work. I need some help on how to have it work with the scripted third person camera.

Note: I have also tried to seperate both the camera bobble and third person camera into two scripts rather than one and it still did not work.

Here is the code for the third person camera:

-- Services

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local context = game:GetService("ContextActionService")

-- Player + Char
local plr = game:GetService("Players").LocalPlayer

-- Other
local Camera = game:GetService("Workspace").CurrentCamera
local mouse = plr:GetMouse()
local cameraDB = false
local zoomDB = false

-- Angles

local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(2,0,8.5)


-- CAMERA SETTING --

wait(0.01)
Camera.CameraType = Enum.CameraType.Scriptable

-- FUNCTIONS --

context:BindAction("CameraMovement", function(_,_,input)
    xAngle = xAngle - input.Delta.x*0.4
    yAngle = math.clamp(yAngle - input.Delta.y*0.4,-80,80)
end, false, Enum.UserInputType.MouseMovement)

rs.RenderStepped:Connect(function()
    local c = plr.Character or plr.CharacterAdded:Wait()
    local rootPart = c:FindFirstChild("HumanoidRootPart")

    if c and rootPart then
        local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

        local cameraCFrame = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
        local cameraFocus = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))

        Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
    end
end)

Here is the code for the bobble effect:

--Bobble

local char = script.Parent
local humanoid = char:WaitForChild('Humanoid')

function Effect()
    local time = tick()
    if humanoid.MoveDirection.Magnitude > 0 then
        local X = math.cos(time*10)*0.35
        local Y = math.abs(math.sin(time*10))*0.35
        local shake = Vector3.new(X,Y,0)
        humanoid.CameraOffset = humanoid.CameraOffset:lerp(shake,0.25)
    else
        humanoid.CameraOffset = humanoid.CameraOffset*0.75
    end
end

rs.RenderStepped:connect(Effect)

This script is placed in StarterPlayerScripts.

This is also in one script, let me know if I should split it into two different scripts if it functions better.

Answer this question