PcoWSkbVqDnWTu_dm2ix

GUI Animations

GUI Animations

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:

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:

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

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:

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

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:

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:

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:

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:

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:

Tags:
  • gui
  • animation
  • tween