Introduction
BetterTween is an improved version of a module I made a while ago for a friend. After making the basic framework of BetterTween for said friend I received multiple requests to open source it so I decided to package it up into a easy to use module for public use.
notable features:
Tween to moving object
tween along 3 point Bezier curve
.Touched works mid tween
scrub through tween
features I want to implement:
ability to tween values other then cframe
tween along Bezier curves exceeding 3 points
tween along custom equations
usage
first, download the module here, or get it off github here.
There are 5 main functions and 1 constructor within the module. These are shown below along with definitions:
local BetterTween = require(game.ReplicatedStorage.BetterTween) --requires module.
local Tween1 = BetterTween.new(PartToTween, StartingCF, EndingCF, TweenInfo, Waypoint) --construct new tween. TweenInfo and Waypoint are both optional.
Tween1:Start() --play/start tween
Tween1:Pause() --pause tween
Tween1:Resume() --resume tween
Tween1:Cancel() --abort tween, if you dont plan on resuming a tween use this instead of pause to save resources.
Tween1:Scrub(ScrubPoint) --basically lerp but for a tween. ScrubPoint is a number between 0 and 1, 0 is 0% through the tween and 1 is 100%.
allow me to explain the constructor parameters better. We have 5 parameters, 2 of which are optional.
PartToTween
: This is the part you want to actually tween. This is what gets moved throughout the tween.
StartingCF
: Starting CFrame for your tween. 99% of the time it will be PartToTween.CFrame
.
EndingCF
: CFrame you want your part to end at; this can either be a part or a CFrame. If you plan on having a dynamic end position use a part as moving the part will also update the tween trajectory and cause the PartToTween
to adjust. If you want a static end point pass a CFrame as that CFrame will not be changed and so the tween trajectory will remain constant.
TweenInfo
(optional): this is the TweenInfo you want to use. If no info is specified it will be set to a blank TweenInfo.new()
.
Waypoint
(optional): This is an optional CFrame you can include if you want to tween across a Bezier curve. This acts as the middle point which will modify the trajectory, it does not effect the StartingCF
or EndingCF
at all.
adding .Touched events and other fun stuff
after creating a new BetterTween you can reference the part you are tweening with (BetterTween).target
; you can then use this to set up .Touched
events and other cool stuff.
code examples
here are some basic use case examples.
create tween with Bezier curve and dynamic endpoint and then play it
local BetterTween = require(game.ReplicatedStorage.BetterTween)
local Tween1 = BetterTween.new(
workspace.Part1,
workspace.Part1.CFrame,
workspace.Part2,
TweenInfo.new(3,Enum.EasingStyle.Exponential,Enum.EasingDirection.In),
workspace.Part3.CFrame
)
Tween1:Start()
create tween, play it, then pause and scrub through it
local BetterTween = require(game.ReplicatedStorage.BetterTween)
local Tween1 = BetterTween.new(
workspace.Part1,
workspace.Part1.CFrame,
workspace.Part2.CFrame,
TweenInfo.new(3,Enum.EasingStyle.Exponential,Enum.EasingDirection.In)
)
coroutine.wrap(function()
Tween1:Start()
end)()
wait(2.5)
Tween1:Pause()
wait(1)
Tween1:Scrub(0.2)
wait(1)
Tween1:Scrub(0.3)
Tween1:Cancel()
create tween then setup .Touched event for it
local BetterTween = require(game.ReplicatedStorage.BetterTween)
local Tween1 = BetterTween.new(
workspace.Part1,
workspace.Part1.CFrame,
workspace.Part2.CFrame,
TweenInfo.new(3,Enum.EasingStyle.Exponential,Enum.EasingDirection.In)
)
Tween1.target.Touched:Connect(function()
print("Touched")
end)
Tween1:Start()
putting it all together
local BetterTween = require(game.ReplicatedStorage.BetterTween)
local Tween1 = BetterTween.new(
workspace.Part1,
workspace.Part1.CFrame,
workspace.Part2,
TweenInfo.new(3,Enum.EasingStyle.Exponential,Enum.EasingDirection.In),
workspace.Part3.CFrame
)
Tween1.target.Touched:Connect(function()
print("Touched")
end)
coroutine.wrap(function()
Tween1:Start()
end)()
wait(1.5)
Tween1:Pause()
wait(2)
Tween1:Resume()
wait(1)
Tween1:Pause()
wait(1)
Tween1:Scrub(0.2)
wait(1)
Tween1:Scrub(0.3)
Tween1:Cancel()
thank you for reading this all if you got this far! Any questions/comments/issues? Feel free to comment!