Potential changes to my motion blur?

Hello! I’ve recently got back into LUA scripting and I was trying to make a motion blur script.
I think my script works pretty well, but I’d like to know people’s opinions on it and ideas on how to improve it.

I thought I could improve it by making it slowly fade in instead of appearing instantly, but I’m not sure on how to do that. Opinions, ideas and improvements?

local context = game:GetService("ContextActionService")
local blureffect = game.Lighting.Blur
savex = 0
savey = 0

function blur(x,y)
	if x ~= savex or y ~= savey then
		blureffect.Size = math.abs(savex - x + savey - y)
	else
		blureffect.Size = 0
	end
	savex = x
	savey = y
	wait(0.01)
	if savex == x and savey == y then
		blureffect.Size = 0
	end
end

context:BindAction("CameraMovement", function(_,_,input)
    blur(input.Delta.X,input.Delta.Y)
end, false, Enum.UserInputType.MouseMovement)

Effect:

3 Likes

Hey! Nice motion blur.

Before I proceed, I’d like to tell you to always try to organize your code. Make it as readable and beautiful as you can. This is of course up to your writing style, but you can improve upon that. :wink:

You can organize direct conditional if statements using a bicondition check, as for example:

local x = 1

print(x == 1 and "x is 1!" or "x is not 1")

This code, for example, would print x is 1! because the condition x == 1 is met.
In a and b or c, a is the condition, and b is what occurs if said condition is true, and c is what occurs if said condition is false.

local context = game:GetService("ContextActionService")
local blureffect = game:GetService("Lighting").Blur
local savex, savey = 0, 0

function blur(x,y)
	blureffect.Size = (x ~= savex or y ~= savey) and math.abs(savex - x + savey - y) or 0
	
	savex, savey = x, y
	
	wait(0.01)
	
	blureffect.Size = (savex == x and savey == y) and 0 or blureffect.Size
end

context:BindAction("CameraMovement", function(_,_,input)
	blur(input.Delta.X, input.Delta.Y)
end, false, Enum.UserInputType.MouseMovement)

As for the smooth effect you’re trying to achieve, you can use the TweenService:
https://developer.roblox.com/en-us/api-reference/class/TweenService

1 Like

Thanks for the reply! I’m a bit confused about how the changes work (the bi-condition check) but it looks like it’s better to use for cleanliness, so I’ll try to learn that!

About the tween service…
I tried for a few minutes to use it and I’ve had no luck, mostly just errors.
Each tween cancels each other out, and then when your camera stops it is blurred.

local TweenService = game:GetService("TweenService")
local context = game:GetService("ContextActionService")
local blureffect = game.Lighting.Blur
local savex, savey = 0, 0
local goal = {}
local info = TweenInfo.new(0)

function blur(x,y)
goal.Size = math.abs(savex - x + savey - y)
info = TweenInfo.new(0.1,Enum.EasingStyle.Linear,Enum.EasingDirection.In)
local bluring = TweenService:Create(blureffect, info, goal)
bluring:Play()
savex, savey = x, y
wait(0.01)
blureffect.Size = (savex ~= x or savey ~= y) and 0 or blureffect.Size
end

context:BindAction("CameraMovement", function(_,_,input)
blur(input.Delta.X,input.Delta.Y)
end, false, Enum.UserInputType.MouseMovement)

For now, I’m going to use the previous script without smoothing since it works, thanks for all the tips!

Please don’t do that in this case, this drastically dropped the readability of the code at a glance; it’s preferable you only do this for simple expressions.

3 Likes