# How to oscillate the y value during a position tween

Im trying to move an object from position ‘a’ to ‘b’ in x, z cordinates, I want the Y cordinate to remain the same as the original y value (when it reaches the end) , yet while the object is moving have the y value oscillate at the absolute value of sin(x) ( or something where it bounces up and down )

intended y result:

Im currently trying to tween the object yet since you cant tween twice at the same time I dont understand how I can correctly oscillate the y value

3 Likes
``````local cycleLength = 3 -- How long each cycle of the oscillation is
local oscillationRange = 3 -- Height of the oscillation

local start = os.clock()
game:GetService("RunService").Hearbeat:Connect(function()
local y = math.abs(math.sin(start / cycleLength)) * oscillationRange
end)
``````
• `math.sin` to get the sin value
• `math.abs` to get the absolute sine value

Use this over the course of the animation or tween.

1 Like

You can’t do this easily with TweenService. Only straight line movement is supported. See this old topic for more info:

1 Like

I just ended up putting it in a for loop with the amount of bounces you want, with a tween that goes up half a bounce distance and then another tween that goes down the multiply by I for the next bounce and such
just realized I forgot to post the code

``````local TweenService = game:GetService("TweenService")

local part = Instance.new("Part")

part.Position = Vector3.new(0, 0, 0)
part.Anchored = true
part.Parent = game.Workspace

local finalPoint = Vector3.new(100,0,150)

local goal = {}

local tweenInfoUp = TweenInfo.new( --
1,
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out

)

local tweenInfoDown = TweenInfo.new(
1,
Enum.EasingStyle.Sine,
Enum.EasingDirection.In
)
-- having two of these should I think handle the velocity it does really matter just adjust to your pleasure

local distance = math.sqrt(finalPoint.X^2 + finalPoint.Z^2) -- a^2 + b^2 = c^2

local amountOfBounces = math.floor(distance/10) -- this is to sort out the use of bigger and smaller bounces only need if its a random point your bouncing between
-- otherwise you can set it to a number you want
local distancePerBounce = finalPoint/amountOfBounces

print(part.Name .. " is travelling " .. distance .. " Studs In " .. amountOfBounces .. " Bounces")
for i = 1, amountOfBounces do

goal.Position = i*distancePerBounce - Vector3.new(distancePerBounce.X/2,-5,distancePerBounce.Z/2)

local tweenUp = TweenService:Create(part, tweenInfoUp, goal)

goal.Position = i*distancePerBounce
local TweenDown = TweenService:Create(part,tweenInfoDown, goal)

tweenUp:Play()

tweenUp.Completed:Wait()

TweenDown:Play()

TweenDown.Completed:Wait()

end

``````