I was looking to make a simple UI shake effect, but I wasn’t sure how to go about it.
I could just make a bunch of tweens to move it around a small space, giving it the effect, but it would probably be inefficient and wouldn’t have the effect I’m trying to achieve.
Here’s a rough code on how you would normally shake the UI:
local originalPosition = script.Parent.Position
local intensity = 15
local speed = .05
while true do --you probably shouldn't use a loop, im just using this as an example
script.Parent.Position = originalPosition + UDim2.fromOffset(math.random(-intensity, intensity), math.random(-intensity, intensity))
-- ^^^ you can also tween this instead for a more fluid feel
task.wait(speed)
end
script.Parent.Position = originalPosition --after we're done we set it back to the original position, of course in this example we're never done, but if you plan to stop it you must do this
local RunServ = game:GetService("RunService")
local Pos = script.Parent.Position
RunServ.RenderStepped:Connect(function(dt)
local BobbleX = (math.cos(os.clock() * 20) * 0.01) -- Increase 20 for an intense shake. Increase decimal number for how far it goes.
local BobbleY = math.abs(math.sin(os.clock() * 30) * 0.01) -- Increase 30 for an intense shake. Increase decimal number for how it goes.
script.Parent.Position = Pos + UDim2.new(BobbleX,0,BobbleY,0) -- Adding pos to the elements, so we can keep the UDIM2 relative to its original position.
end)