I’ve been doing a lot of testing recently involving the microprofiler to look for sources of lag in my game. I have pinpointed the issue down to raycasting in my custom mouse handler. This is the entire event code for updating the mouse’s properties:
RunService:BindToRenderStep("MouseUpdate", 1, function()
local position = Mouse:GetPosition()
local unitRay = camera:ViewportPointToRay(position.X, position.Y)
if not ignoreList.Character then
ignoreList.Character = Player.Character
end
-- This isn't causing much lag but it's noticeable when looking at the microprofiler
debug.profilebegin("MouseFiltering")
raycastParams.FilterDescendantsInstances = Funcs.ConcatTables(Mouse.TargetFilter, ignoreList)
debug.profileend("MouseFiltering")
-- Raycasting is creating the majority of the lag
debug.profilebegin("Raycasting")
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * lengthCap, raycastParams)
debug.profileend("Raycasting")
if raycastResult then
Mouse.Target = raycastResult.Instance
Mouse.Hit = CFrame.new(raycastResult.Position)
Mouse.HitNormal = raycastResult.Normal
end
end)
The whole point of creating this mouse handler was to recreate the default Roblox mouse object but with better filtering for Mouse.Hit
, Mouse.Target
, and a new Mouse.HitNormal
value. Considering how what makes it function is creating lag, is there a better way of doing this? Or maybe a key modification that I missed?