I want to keep player camera control and on top of it add camera shake effects, once hit by a zombie.
For camera shake I use EZ camera shake module:
EZ camera shake module
The camera control looks like this:
The code for this camera control:
local Player = game:GetService("Players").LocalPlayer
Player.CharacterAppearanceLoaded:Wait()
local player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local camera = workspace.CurrentCamera
local runService = game:GetService("RunService")
local screenGui = game.StarterGui.ScreenGui
-- here is your template for Vector3 axis (x y z)
local Distance = Vector3.new(0,25,0) -- Distance from player
local Direction = Vector3.new(0,-1,0) -- Direction from which camera will be looking at player
local CameraSensitivity = 5 -- change it if you want that camera to lean further (give it a bigger number)
-- or if want an effect to be more subtle decrease the number. (if you set it as a negative, camera will lean in the opposite direction)
function getMouseScreenPositionCentered()
return Vector2.new(
mouse.X - screenGui.AbsoluteSize.X/2,
mouse.Y - screenGui.AbsoluteSize.Y/2)
end
function pixelToFraction(pixelV2)
--Turns a coordinate in pixels into a coordinate in some fraction of the size of the screen
return pixelV2 / screenGui.AbsoluteSize
end
function onRenderStep()
if player.Character then
local rootPart = player.Character.HumanoidRootPart
local playerposition = player.Character.HumanoidRootPart.Position + Vector3.new(0,0,3) -- sometimes camera will be offset to your character,
local cameraoffset = Distance + playerposition
local mouseScreenPos = pixelToFraction( getMouseScreenPositionCentered() )
local Axis = Vector3.new(-mouseScreenPos.Y,0,mouseScreenPos.X)
local cameraPos = cameraoffset + Axis * CameraSensitivity
camera.CFrame = CFrame.new(cameraPos, cameraPos + Direction)
end
end
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)
The bug (in the presented video only the key “S” is pressed):
The camera shake is activated by this remote event local script:
local CameraShaker = require(game.ReplicatedStorage.CameraShaker)
local Player = game:GetService("Players").LocalPlayer
Player.CharacterAppearanceLoaded:Wait()
local camera = workspace.CurrentCamera
local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
camera.CFrame = camera.CFrame * shakeCf
end)
camShake:Start()
game.ReplicatedStorage.Player_Events.Cam_Shake.OnClientEvent:Connect(function(Type)
if Type == "Explosion" then
print(Enum.RenderPriority.Camera.Value)
camShake:Shake(camShake.Presets.BadTrip)
end
end)
Thanks in advance!