can you add camera shake to this roblox script that gets more faster/intense the more…
- User
can you add camera shake to this roblox script that gets more faster/intense the more walkspeed the player has (default walkspeed is 16) and make it so u can change the intensity of the shake in the script here is the script:
-- StarterGui --
-- Place this into StarterGui or StarterPack -- -- CREDIT -- -- EvelienSterk; for the initial script -- -- SawyerDGamer; some stuff ripped from his panner script -- -- iytgggggg; shoving it all into this script and then some :) -- -- UPDATE: turned it into r15, made it so you can swim right in water --
repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil local runService = game:GetService("RunService") local input = game:GetService("UserInputService") local players = game:GetService("Players")
CanToggleMouse = {allowed = true; activationkey = Enum.KeyCode.F;} -- lets you move your mouse around in firstperson CanViewBody = true -- whether you see your body Sensitivity = 0.2 -- anything higher would make looking up and down harder; recommend anything between 0
1 Smoothness = 0.05 -- recommend anything between 01 FieldOfView = 90 -- fovlocal cam = game.Workspace.CurrentCamera local player = players.LocalPlayer local m = player:GetMouse() m.Icon = "http://www.roblox.com/asset/?id=569021388" -- replaces mouse icon local character = player.Character or player.CharacterAdded:wait() local humanoidpart = character.HumanoidRootPart
local head = character:WaitForChild("Head") local CamPos,TargetCamPos = cam.CoordinateFrame.p,cam.CoordinateFrame.p local AngleX,TargetAngleX = 0,0 local AngleY,TargetAngleY = 0,0
local running = true local freemouse = false
function updatechar()
for _, v in pairs(character:GetChildren())do if CanViewBody then if v.Name == 'Head' then v.LocalTransparencyModifier = 1 v.CanCollide = false end else if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then v.LocalTransparencyModifier = 1 v.CanCollide = false end end if v:IsA'Accessory' then v:FindFirstChild('Handle').LocalTransparencyModifier = 1 v:FindFirstChild('Handle').CanCollide = false end if v:IsA'Hat' then v:FindFirstChild('Handle').LocalTransparencyModifier = 1 v:FindFirstChild('Handle').CanCollide = false end endend
input.InputChanged:connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then local delta = Vector2.new(inputObject.Delta.x/Sensitivity,inputObject.Delta.y/Sensitivity) * Smoothness local X = TargetAngleX - delta.y TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X TargetAngleY = (TargetAngleY - delta.x) %360 endend)
input.InputBegan:connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.Keyboard then if inputObject.KeyCode == CanToggleMouse.activationkey then if CanToggleMouse.allowed and freemouse == false then freemouse = true else freemouse = false end end endend)
runService.RenderStepped:connect(function()
if running then updatechar() CamPos = CamPos + (TargetCamPos - CamPos) *0.28 AngleX = AngleX + (TargetAngleX - AngleX) *0.35 local dist = TargetAngleY - AngleY dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist AngleY = (AngleY + dist *0.35) %360 cam.CameraType = Enum.CameraType.Scriptable cam.CoordinateFrame = CFrame.new(head.Position) * CFrame.Angles(0,math.rad(AngleY),0) * CFrame.Angles(math.rad(AngleX),0,0) * CFrame.new(0,0.8,0) -- offset humanoidpart.CFrame=CFrame.new(humanoidpart.Position)*CFrame.Angles(0,math.rad(AngleY),0) character.Humanoid.AutoRotate = false else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default; character.Humanoid.AutoRotate = true end if (cam.Focus.p-cam.CoordinateFrame.p).magnitude < 1 then running = false else running = true if freemouse == true then game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter end end if not CanToggleMouse.allowed then freemouse = false end cam.FieldOfView = FieldOfViewend)
- Assistant
Sure! I've added camera shake to your Roblox script that intensifies as the player's walkspeed increases beyond the default walkspeed of 16. I've also included a variable
ShakeIntensityso you can adjust the overall intensity of the shake directly in the script.Here is the modified script:
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- -- StarterGui -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Place this into StarterGui or StarterPack -- -- CREDIT -- -- EvelienSterk; for the initial script -- -- SawyerDGamer; some stuff ripped from his panner script -- -- iytgggggg; shoving it all into this script and then some :) -- -- UPDATE: turned it into r15, made it so you can swim right in water -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- repeat wait() until game:GetService("Players").LocalPlayer.Character ~= nil local runService = game:GetService("RunService") local input = game:GetService("UserInputService") local players = game:GetService("Players") CanToggleMouse = {allowed = true; activationkey = Enum.KeyCode.F;} -- lets you move your mouse around in firstperson CanViewBody = true -- whether you see your body Sensitivity = 0.2 -- anything higher would make looking up and down harder; recommend anything between 0~1 Smoothness = 0.05 -- recommend anything between 0~1 FieldOfView = 90 -- fov BaseSpeed = 16 -- default walkspeed ShakeIntensity = 1 -- adjust this to change overall shake intensity local cam = game.Workspace.CurrentCamera local player = players.LocalPlayer local m = player:GetMouse() m.Icon = "http://www.roblox.com/asset/?id=569021388" -- replaces mouse icon local character = player.Character or player.CharacterAdded:wait() local humanoidpart = character.HumanoidRootPart local humanoid = character:WaitForChild("Humanoid") local head = character:WaitForChild("Head") local CamPos,TargetCamPos = cam.CoordinateFrame.p,cam.CoordinateFrame.p local AngleX,TargetAngleX = 0,0 local AngleY,TargetAngleY = 0,0 local running = true local freemouse = false ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- function updatechar() for _, v in pairs(character:GetChildren()) do if CanViewBody then if v.Name == 'Head' then v.LocalTransparencyModifier = 1 v.CanCollide = false end else if v:IsA'Part' or v:IsA'UnionOperation' or v:IsA'MeshPart' then v.LocalTransparencyModifier = 1 v.CanCollide = false end end if v:IsA'Accessory' then v:FindFirstChild('Handle').LocalTransparencyModifier = 1 v:FindFirstChild('Handle').CanCollide = false end if v:IsA'Hat' then v:FindFirstChild('Handle').LocalTransparencyModifier = 1 v:FindFirstChild('Handle').CanCollide = false end end end ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- input.InputChanged:connect(function(inputObject) if inputObject.UserInputType == Enum.UserInputType.MouseMovement then local delta = Vector2.new(inputObject.Delta.x/Sensitivity, inputObject.Delta.y/Sensitivity) * Smoothness local X = TargetAngleX - delta.y TargetAngleX = (X >= 80 and 80) or (X <= -80 and -80) or X TargetAngleY = (TargetAngleY - delta.x) % 360 end end) input.InputBegan:connect(function(inputObject) if inputObject.UserInputType == Enum.UserInputType.Keyboard then if inputObject.KeyCode == CanToggleMouse.activationkey then if CanToggleMouse.allowed and freemouse == false then freemouse = true else freemouse = false end end end end) ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- runService.RenderStepped:connect(function() if running then updatechar() CamPos = CamPos + (TargetCamPos - CamPos) * 0.28 AngleX = AngleX + (TargetAngleX - AngleX) * 0.35 local dist = TargetAngleY - AngleY dist = math.abs(dist) > 180 and dist - (dist / math.abs(dist)) * 360 or dist AngleY = (AngleY + dist * 0.35) % 360 cam.CameraType = Enum.CameraType.Scriptable -- Get the player's current walkspeed local walkspeed = humanoid.WalkSpeed local speedRatio = (walkspeed - BaseSpeed) / BaseSpeed if speedRatio < 0 then speedRatio = 0 end local shakeAmount = speedRatio * ShakeIntensity -- Generate random shake values local shakeAngleX = math.rad((math.random() - 0.5) * 2 * shakeAmount) local shakeAngleY = math.rad((math.random() - 0.5) * 2 * shakeAmount) local shakeAngleZ = math.rad((math.random() - 0.5) * 2 * shakeAmount) local shakeOffsetX = ((math.random() - 0.5) * 2 * shakeAmount * 0.1) local shakeOffsetY = ((math.random() - 0.5) * 2 * shakeAmount * 0.1) local shakeOffsetZ = ((math.random() - 0.5) * 2 * shakeAmount * 0.1) -- Create the shake CFrame local shakeCFrame = CFrame.new(shakeOffsetX, shakeOffsetY, shakeOffsetZ) * CFrame.Angles(shakeAngleX, shakeAngleY, shakeAngleZ) -- Set the camera's coordinate frame with shake cam.CoordinateFrame = CFrame.new(head.Position) * CFrame.Angles(0, math.rad(AngleY), 0) * CFrame.Angles(math.rad(AngleX), 0, 0) * CFrame.new(0, 0.8, 0) -- offset * shakeCFrame humanoidpart.CFrame = CFrame.new(humanoidpart.Position) * CFrame.Angles(0, math.rad(AngleY), 0) character.Humanoid.AutoRotate = false else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default character.Humanoid.AutoRotate = true end if (cam.Focus.p - cam.CoordinateFrame.p).magnitude < 1 then running = false else running = true if freemouse == true then game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.Default else game:GetService("UserInputService").MouseBehavior = Enum.MouseBehavior.LockCenter end end if not CanToggleMouse.allowed then freemouse = false end cam.FieldOfView = FieldOfView end) ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----Explanation of the Changes:
-
Variables Added:
BaseSpeed: The default walkspeed, set to 16.ShakeIntensity: A variable you can adjust to change the overall intensity of the camera shake.
-
Humanoid Reference:
- Added
local humanoid = character:WaitForChild("Humanoid")to reference the player's humanoid.
- Added
-
Camera Shake Logic:
- Inside the
RenderSteppedfunction, after checking ifrunningistrue, the script now calculates the player's current walkspeed and determines the shake intensity based on the increase from the base speed. - Random shake angles and offsets are generated using
math.random()and scaled byshakeAmount. - A
shakeCFrameis created combining the shake offsets and angles. - The
shakeCFrameis applied to the camera'sCoordinateFrame, adding the shake effect to the camera.
- Inside the
-
Adjusting the Shake:
- You can adjust the
ShakeIntensityvariable at the top of the script to increase or decrease the overall shake effect. - The shake intensifies as the player's walkspeed increases beyond the
BaseSpeed. If the walkspeed is at or below the base speed, there will be no shake.
- You can adjust the
How It Works:
-
Calculating Shake Amount:
speedRatio = (walkspeed - BaseSpeed) / BaseSpeedcalculates how much faster the player is moving relative to the base speed.- If the walkspeed is less than or equal to the base speed,
speedRatiobecomes0, resulting in no shake. - The
shakeAmountis then calculated by multiplyingspeedRatiowithShakeIntensity.
-
Applying Shake to the Camera:
- Random small rotations (
shakeAngleX,shakeAngleY,shakeAngleZ) and position offsets (shakeOffsetX,shakeOffsetY,shakeOffsetZ) are generated. - These values are used to create a
shakeCFrame, which represents the camera shake transformation. - The
shakeCFrameis then applied to the camera'sCoordinateFrameto add the shake effect.
- Random small rotations (
Adjusting the Intensity:
- To make the shake stronger or weaker, you can change the value of
ShakeIntensity. For example:ShakeIntensity = 0.5will make the shake less intense.ShakeIntensity = 2will make the shake more intense.
Example Usage:
- If you want the shake to be more noticeable at higher speeds, you can increase
ShakeIntensity. - If you want the shake to start at a different base speed, adjust
BaseSpeedaccordingly.
Feel free to adjust the
ShakeIntensityandBaseSpeedvalues to suit your game's needs. If you have any questions or need further assistance, let me know! -