GSmGpE6CwX2y9JjB25B8
We use cookies on this site to enhance your user experience

GUI Animations

GUI Animations

Aug 29 2019, 9:15 AM PST 5 min

In animation, tweening is an abbreviation for “in-betweening,” the process of generating intermediate frames between two key points in a sequence. When designing a user interface, tweening can be used to transition a GUI smoothly from one position/state to another, for instance:

  • Smoothly increasing the size of a “selected” button in a series of related option buttons.
  • Sliding GUI menus in and out from the screen edges.
  • Gradually animating a health bar between two widths when a health boost is received.

Basic Tweening

In the articles/Intro to GUIs|Intro to GUIs and articles/Creating GUI Buttons|Creating GUI Buttons articles, you learned how to create basic text labels and interactive buttons. These and other GUIs like Frame and ScrollingFrame can all be tweened via a script attached directly to the object.

  1. In the Explorer window, hover over the StarterGui object, click on the circle button, and insert a ScreenGui object.
  2. Select the new ScreenGui object and, in a similar manner, insert a TextButton.
  3. Select the new TextButton and insert a new LocalScript.
  1. Copy and paste the following code into the script:
local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)

With these variables in place, you can now tween the GUI with built-in methods of its base GuiObject class.

Position

The GUI position can be tweened using the GuiObject/TweenPosition|GuiObject:TweenPosition() method. In its most basic form, this method requires an end position (datatype/UDim2) for the object’s destination:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0.1, 0, 0.5, 0)

wait(2)

object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))

Alternatively, you can make the GUI start off the left edge of the screen by offsetting the object’s starting position by its width:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, -object.Size.X.Offset, 0.5, 0)

wait(2)

object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0))

Size

A GUI’s size can be tweened using the GuiObject/TweenSize|GuiObject:TweenSize() method which accepts a datatype/UDim2 for the object’s final size:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0.5, 0, 0.5, 0)

wait(2)

object:TweenSize(UDim2.new(0.4, 0, 0.4, 0))

The above example uses the scale parameters of datatype/UDim2, but you can also resize the GUI using explicit size parameters:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0.5, 0, 0.5, 0)

wait(2)

object:TweenSize(UDim2.new(0, 400, 0, 100))

Position and Size

To tween both the position and size of a GUI in one command, use the GuiObject/TweenSizeAndPosition|GuiObject:TweenSizeAndPosition() method. This requires a datatype/UDim2 for both the final size and position of the GUI:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, -object.Size.X.Offset, 0.5, 0)

wait(2)

object:TweenSizeAndPosition(UDim2.new(0.4, 0, 0.4, 0), UDim2.new(0.5, 0, 0.5, 0))

Additional Options

All of the above methods accept additional options which can fine-tune or provide more control over GUI animations.

Animation Easing

Animation easing defines a “direction” and style in which the tween will occur.

Direction Description
In The tween will have less speed at its beginning and more speed toward its end.
Out The tween will have more speed at its beginning and less speed toward its end.
InOut In and Out on the same tween, with In at the beginning and Out taking effect halfway through.
Style Description
Linear Moves at a constant speed.
Sine Movement speed is determined by a sine wave.
Back Tween movement backs into or out of place.
Quad Eases in or out with quadratic interpolation.
Quart Similar to Quad but with a more emphasized start and/or finish.
Quint Similar to Quad but with an even more emphasized start and/or finish.
Bounce Moves as if the start or end position of the tween is bouncy.
Elastic Moves as if the object is attached to a rubber band.

Easing options can be defined as Enum/EasingDirection and Enum/EasingStyle enums after the size and/or position datatype/UDim2 values:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, 0, 0.5, 0)
 
wait(2)
 
object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint)

Time

By default, a tween will occur in 1 second, but this can be adjusted by specifying a number of seconds following the easing options:

local object = script.Parent
object.AnchorPoint = Vector2.new(0.5, 0.5)
object.Position = UDim2.new(0, 0, 0.5, 0)
 
wait(2)
 
object:TweenPosition(UDim2.new(0.5, 0, 0.5, 0), nil, nil, 3)

Advanced Tweening

The methods above are simple ways to tween a GUI’s position and size, but they cannot tween other aspects like rotation, color, or transparency. For these, you’ll need to use TweenService, a powerful tool which can tween almost any property or combination of properties.

Color Tween

Some animations require a tween of a datatype/Color3 property, for instance changing a health bar’s color from green to yellow if a player’s health falls below a certain percentage.

  1. In the Explorer window, hover over the ScreenGui object you created earlier and insert a Frame.
  2. Select the new frame object and insert a new LocalScript.
  3. Copy and paste the following code into the script:
local TweenService = game:GetService("TweenService")

local frame = script.Parent
frame.Position = UDim2.new(0, 20, 0, 20)
frame.BorderSizePixel = 0
frame.Size = UDim2.new(0, 400, 0, 30)
frame.BackgroundColor3 = Color3.fromRGB(0, 255, 75)

-- Declare target size and color values
local newSize = UDim2.new(0, frame.Size.X.Offset*0.5, 0, frame.Size.Y.Offset)
local newColor = Color3.fromRGB(255, 255, 50)

-- Set up tween
local tweenInfo = TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.Out)
local tween = TweenService:Create(frame, tweenInfo, {Size=newSize, BackgroundColor3=newColor})

wait(2)

tween:Play()

This code does the following:

  • On lines 4-7, sets the frame’s position, border, size, and background color (green).
  • On lines 10-11, declares a target size and background color.
  • On line 14, declares a new datatype/TweenInfo object for 1.5 seconds with Out easing.
  • On line 15, creates a new Tween which interpolates the frame’s GuiObject/Size|Size and GuiObject/BackgroundColor3|BackgroundColor3 to newSize and newColor respectively.
  • Plays the tween on line 19.

Sequential Tween

Some animations may benefit from a sequential setup, for example a rotation tween following a movement tween. This can be done by starting the second tween following the first tween’s TweenBase/Completed|Completed event:

local TweenService = game:GetService("TweenService")

local frame = script.Parent
frame.AnchorPoint = Vector2.new(0.5, 0.5)
frame.Position = UDim2.new(0, 70, 0, 70)
frame.BorderSizePixel = 0
frame.Size = UDim2.new(0, 100, 0, 100)
frame.BackgroundColor3 = Color3.new(0, 0, 0)

-- Set up tweens
local tweenInfo1 = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local tween1 = TweenService:Create(frame, tweenInfo1, {Position=UDim2.new(0.5, 0, 0.5, 0)})
local tweenInfo2 = TweenInfo.new(1.5, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut)
local tween2 = TweenService:Create(frame, tweenInfo2, {Rotation=180})

tween1.Completed:Connect(function()
	tween2:Play()
end)

wait(2)

tween1:Play()
Tags:
  • gui
  • animation
  • tween