Getting a part to 'bob' or bounce


I wanna make this arrow (it’s a union) bob or bounce up and down. I’ve tried the different ‘Body’ things but none seem to achieve what I’m after

6 Likes

Anchor the object and use TweenService to tween its CFrame back and forth.

9 Likes

That’s one way. If you want to drive it from code you can use something like:

–Inside something called every frame
local speed = 1
local amplitude = 10
phase = phase + deltaTime * speed

local position = originalPosition + Vector3.new(0, math.sin( phase ) * amplitude, 0)

part.CFrame = CFrame.new(position)

2 Likes

to make it bounce use the above but change this part

originalPosition + Vector3.new(0, math.abs(math.sin( phase ) * amplitude), 0)

You can also as previously stated use a looping tween.

1 Like

What’s deltaTime

1 Like

TweenService is your friend, makes it so easy! Here’s some sample code, taken from here with very little modification/addition to make it work. You can also play with the easing styles and direction, time, etc. Can also be a local or network part depending on what script you put it in.

local TweenService = game:GetService("TweenService")
 
local Arrow = game.ReplicatedFirst.Arrow:Clone()
Arrow.Parent = workspace

local tweenInfo = TweenInfo.new(
	0.5, -- Time
	Enum.EasingStyle.Linear , -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	true, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

local NewPosition = Arrow.Position + Vector3.new(0, -5, 0)
 
local tween = TweenService:Create(Arrow, tweenInfo, {Position = NewPosition})

tween:Play()
wait(30)
tween:Cancel()
18 Likes

Guessing I’d just remove the Cancel so it just never ends?

Correct and happy birthday

2 Likes

You would also probably want to change these two lines:

Enum.EasingStyle.Linear
local NewPosition = Arrow.Position + Vector3.new(0, -5, 0)

To these two lines:

Enum.EasingStyle.Sine
local NewPosition = Arrow.Position + Vector3.new(0, 5, 0)

As it will give you a much better “bouncing” effect.
(And happy birthday!)

2 Likes

I see this is an old post, but if someone looking at this topic is still looking for a solution, I ran this loop based on RunService.Heartbeat and used the step amount as my deltaTime (change in time)

local speed = 100
local amplitude = 0.01
phase = phase + step * speed
movingPart.Position = targetPosition + Vector3.new(0, math.sin(math.rad(phase)) * amplitude, 0)

You can mess around with the speed and amplitude, those values are just what I needed since I only wanted a slight bounce

Also, if you don’t need to use RunService.Heartbeat, my value for step was around 0.16

1 Like

in this context, delta just means “change” or “difference” so deltaTime just means the difference or change in time.

when the code is ran every frame, the deltaTime variable will be different, hence why it’s called deltaTime.