Levitating object animation

Hello developers, I’m searching for help on how to make a part/mesh levitate up and down, and perhaps smoothly, I haven’t exactly learned how to use tweenservice yet, but I’ll be more than happy to learn the basic through this.

Thanks.

**Intention: to make this coin bag float up and down.

1 Like

Try this

local RunService = game:GetService("RunService") -- define RunService, to make lerps (alternative version of tweens) work you will need to run them in a loop

local object = script.Parent -- this is your coin bag, it won't work if it's a model, however, I can modify the script so it works with models, just tell me

local speed = 2
local distance = 0.4 
local smoothness = 0.1 -- from 0 to 1, the closer it is to 0 the smoother it will be

RunService.Heartbeat:Connect(function() -- this function will run in a loop
	local t = tick() -- time
	local y = math.sin(t * speed) * distance -- this is a trigonometric function we will use to get the offset

	object.CFrame = object.CFrame:Lerp(object.CFrame * CFrame.new(0, y, 0), smoothness) -- now just set the cframe
end)

Recorded at 30 fps, it is way smoother

6 Likes

Yeah! TweenService provides all of the tools you need to achieve this.

I made a little recreation of your scenario shown below:

Here is the code:

local TweenService = game:GetService("TweenService")
local part = script.Parent
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweenObject = TweenService:Create(part, tweenInfo, {CFrame = part.CFrame * CFrame.new(0, 5, 0)})
tweenObject:Play()

As you can see, I use TweenService to create the animation. Here is a breakdown of the script.
Line 1: Variable to store the TweenService service
Line 2: Variable to store the part to tween
Line 3:Creation of a TweenInfo object. The TweenInfo object takes in the following arguments (in order):

  • Animation time (in my case, 2 seconds)
  • EasingStyle
  • EasingDirection
  • Number of times to repeat the animation (since I put -1, that would mean it would loop indefinitely)
  • Reverses (I put true, meaning the animation plays in reverse after completing the initial tween)
  • Delay time between animation

You can find more information about TweenInfo in this article.

Line 4: Variable to store the TweenObject which is created from TweenService. TweenService:Create() takes the following arguments:

  • Instance to tween (the part that I want to animate)
  • A TweenInfo object (used the one I created on line 3)
  • A table showing which properties of the instance to tween (I choose to change the CFrame property to the part’s current CFrame +5 in the y direction)

You can find more information about TweenService:Create() here.

Line 5: Plays the created Tween.

Some more helpful articles:
https://developer.roblox.com/en-us/api-reference/class/Tween
https://developer.roblox.com/en-us/api-reference/class/TweenService
https://developer.roblox.com/en-us/api-reference/function/TweenBase/Play
https://developer.roblox.com/en-us/api-reference/enum/EasingStyle
https://developer.roblox.com/en-us/api-reference/enum/EasingDirection

@zilibobi 's approach should also work, but I suggest using TweenService for more concise and efficient code. Also, it’s always fun to take advantage of services that Roblox provides for us!

If you have any further questions, please ask!

1 Like

Thank you very much, I have learned quite a lot.

Nice. Good luck with the project!

1 Like