Med Written By I_Yagyaveer

GUI Animations Created on: 24-05-2020

Basic Tweening, Additional Options, Advanced Tweening And More!

GUI Animations

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

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. Select the new ScreenGui object and, in a similar manner, insert a TextButton. Select the new TextButton and insert a new LocalScript .

(rbxassetid://5081103632:17:6.6)

4.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() method. In its most basic form, this method requires an end position (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))





Note that this tween uses the scale parameters of UDim2 versus the positional offset values. This ensures that the GUI tweens to the center of the screen, no matter the screen's size or orientation.

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() method which accepts a UDim2 for the object’s final size:





The above example uses the scale parameters of 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() method. This requires a 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:-

  • 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:-

  • 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 EasingDirection and EasingStyle enums after the size and/or position 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)

Note that nil can be used for either of the easing options if you don't need to change the default EasingDirection of Out or the default EasingStyle of Quad .

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 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. Select the new frame object and insert a new LocalScript. 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 TweenInfo object for 1.5 seconds with Out easing. On line 15, creates a new Tween which interpolates the frame’s Size and 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 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()

Med Written By I_Yagyaveer

See I_Yagyaveer's profile on Roblox

Discussion