Hello, I am currently trying to make a camera parallax and smooth shake system in my main menu. Currently, the parallax works great, but the shake is giving me an error “Unable to cast value to Object” Any ideas?
Here’s my code.
local MenuCamera = {
Camera = workspace.CurrentCamera,
cameraCFrame = workspace.MapFolder.Other.MenuScene:WaitForChild("CameraPart").CFrame, -- camera part
MaxXRotationParallax = 15,
MaxYRotationParallax = 15,
}
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local TweenService = game:GetService("TweenService")
function MenuCamera:Enable()
if self.Connection then
self.Connection:Disconnect() -- Disconnect from the Mouse.Move event, if exists
end
self.Camera.CameraType = Enum.CameraType.Scriptable
local function MouseMove()
local XFloat = (Mouse.X / self.Camera.ViewportSize.X) - 0.5
local YFloat = (Mouse.Y / self.Camera.ViewportSize.Y) - 0.5
local _CFrame = self.cameraCFrame
* CFrame.fromEulerAnglesYXZ(
math.rad(self.MaxYRotationParallax * -YFloat),
math.rad(self.MaxXRotationParallax * -XFloat),
0
)
local tweenCam = TweenService:Create(
self.Camera,
TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
{ CFrame = _CFrame }
)
tweenCam:Play()
task.wait(1)
end
local function cameraShake()
local XShake = math.round(math.random(0, self.MaxXRotationParallax))
local YShake = math.round(math.random(0, self.MaxYRotationParallax))
local _CFrame = self.cameraCFrame
* CFrame.fromEulerAnglesYXZ(
math.rad(self.MaxYRotationParallax * XShake),
math.rad(self.MaxXRotationParallax * YShake),
0
)
local tweenCam = TweenService:Create(
self.cameraCFrame,
TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),
{ Orientation = _CFrame }
)
tweenCam:Play()
end
self.Connection = Mouse.Move:Connect(MouseMove)
MouseMove()
while true do
cameraShake()
end
end
function MenuCamera:Disable()
if self.Connection then
self.Connection:Disconnect() -- Disconnect from the Mouse.Move event, if exists
end
self.Camera.CameraType = Enum.CameraType.Custom -- Set the CameraType back to default
end
return MenuCamera
This is a module script.