How to gradually increase speed of a sine wave?

Hello, I have been working on an egg hatching system and I am rotating my egg using a smooth sine wave:


And I am trying to make it look like this:

Here is my function for displaying and rotating the egg:

function module:DisplayEgg(eggName)
	local cframe = CFrame.new()
	local egg = workspace['Eggs'][eggName]:Clone()
	egg.Parent = camera
	for _, part in pairs(egg:GetDescendants()) do
		if part:IsA('BasePart') then
			part.CanCollide = false
		end
	end
	local offset = CFrame.new(0,0,-(egg.PrimaryPart.Size.Z * 1.5))
	cframe = offset 
	local x = 8
	local function onRenderStep(dt)
		local angle = 50*math.sin(x*tick())
		local eggCFrame = camera.CFrame * offset * CFrame.Angles(0, 0, math.rad(angle))
		egg:SetPrimaryPartCFrame(eggCFrame)
	end
	connection1 = runService.RenderStepped:Connect(onRenderStep)
end

How can I make a sine wave get faster and faster?
Thank you!

Edit: I tried having a variable for the speed and adding 0.001 to it each time but it gets too fast.

Try adding that like every 10 frames or sth

How do I check how many frames?

Make a variable. += it every RenderStepped. Reset it to 0 if it equals to 10 and to the function.

local FrameCounter = 0
function OnRenderStepped()
   if FrameCounter == 10 then
     FrameCounter = 0
     -- do the function
   else
      FrameCounter += 1
   end
end
2 Likes

That’s what I did:

But, is there a way of doing that every 10 frames?

I did limit it to 10 but it goes up to 10 very fast.

Check the edit please. Let me know if the helps.

I did this but it does not work:

function module:DisplayEgg(eggName)
	local cframe = CFrame.new()
	local egg = workspace['Eggs'][eggName]:Clone()
	egg.Parent = camera
	for _, part in pairs(egg:GetDescendants()) do
		if part:IsA('BasePart') then
			part.CanCollide = false
		end
	end
	local offset = CFrame.new(0,0,-(egg.PrimaryPart.Size.Z * 1.5))
	cframe = offset 
	local x = 1
	local FrameCounter = 0

	local function onRenderStep(dt)
		
		local angle = 20*math.sin(x*tick())
		local eggCFrame = camera.CFrame * offset * CFrame.Angles(0, 0, math.rad(angle))
		egg:SetPrimaryPartCFrame(eggCFrame)
		if FrameCounter == 10 then
			FrameCounter = 0
			x+=1
		else
			FrameCounter += 1
		end
		
	end
	connection1 = runService.RenderStepped:Connect(onRenderStep)
end

Try this: (Sorry for the bad formatting)

function module:DisplayEgg(eggName)
	local cframe = CFrame.new()
	local egg = workspace['Eggs'][eggName]:Clone()
	egg.Parent = camera
	for _, part in pairs(egg:GetDescendants()) do
		if part:IsA('BasePart') then
			part.CanCollide = false
		end
	end
	local offset = CFrame.new(0,0,-(egg.PrimaryPart.Size.Z * 1.5))
	cframe = offset 
	local x = 1
	local FrameCounter = 0

	local function onRenderStep(dt)
		
		
		if FrameCounter == 10 then
local angle = 20*math.sin(x*tick())
		local eggCFrame = camera.CFrame * offset * CFrame.Angles(0, 0, math.rad(angle))
		egg:SetPrimaryPartCFrame(eggCFrame)
			FrameCounter = 0
			x+=1
		else
			FrameCounter += 1
		end
		
	end
	connection1 = runService.RenderStepped:Connect(onRenderStep)
end

When I do that it becomes very choppy:

Can you open the output window and show me?

I opened the output but there were no errors.

This would speed it up:

function module:DisplayEgg(eggName)
	local cframe = CFrame.new()
	local egg = workspace['Eggs'][eggName]:Clone()
	egg.Parent = camera
	for _, part in pairs(egg:GetDescendants()) do
		if part:IsA('BasePart') then
			part.CanCollide = false
		end
	end
	local offset = CFrame.new(0,0,-(egg.PrimaryPart.Size.Z * 1.5))
	cframe = offset 
	local x = 8
    local startTick = tick()
	local function onRenderStep(dt)
        local currentTick = tick()
        local speed = currentTick - startTick + 1
		local angle = 50*math.sin(speed * x * currentTick)
		local eggCFrame = camera.CFrame * offset * CFrame.Angles(0, 0, math.rad(angle))
		egg:SetPrimaryPartCFrame(eggCFrame)
	end
	connection1 = runService.RenderStepped:Connect(onRenderStep)
end

The idea is that you multiply x by a given speed. In this case, the speed is defined as the interval between the start and the current time. That means that the speed increases over time.

1 Like

It does speed it up but how can I limit it’s speed and make it speed up slower?

local speedScale = 1 / 10
local maxSpeed = 5

local interval = (tick() - startTick + 1)
-- Clamp the speed
local speed = math.min(interval * speedScale, maxSpeed)
local angle = 50 * math.sin(speed * x)
1 Like

Thank you! But how do I implement it? I tried this:

function module:DisplayEgg(eggName)
	local cframe = CFrame.new()
	local egg = workspace['Eggs'][eggName]:Clone()
	egg.Parent = camera
	for _, part in pairs(egg:GetDescendants()) do
		if part:IsA('BasePart') then
			part.CanCollide = false
		end
	end
	local offset = CFrame.new(0,0,-(egg.PrimaryPart.Size.Z * 1.5))
	cframe = offset 
	local x = 8
	local startTick = tick()
	
	local function onRenderStep(dt)
		local currentTick = tick()

		
		local speedScale = 1 / 10
		local maxSpeed = 5

		local interval = (currentTick - startTick + 1)
		-- Clamp the speed
		local speed = math.min(interval * speedScale, maxSpeed)
		local angle = 50 * math.sin(interval * speed * x * currentTick)
		
		local eggCFrame = camera.CFrame * offset * CFrame.Angles(0, 0, math.rad(angle))
		egg:SetPrimaryPartCFrame(eggCFrame)
	end
	connection1 = runService.RenderStepped:Connect(onRenderStep)
end
function module:DisplayEgg(eggName)
	local cframe = CFrame.new()
	local egg = workspace['Eggs'][eggName]:Clone()
	egg.Parent = camera
	for _, part in pairs(egg:GetDescendants()) do
		if part:IsA('BasePart') then
			part.CanCollide = false
		end
	end
	local offset = CFrame.new(0,0,-(egg.PrimaryPart.Size.Z * 1.5))
	cframe = offset 
	local x = 8
    local startTick = tick()
    local speedScale = 1 / 10
    local maxSpeed = 5
	local function onRenderStep(dt)
        local interval = tick() - startTick
        -- Clamp the speed
        local speed = math.min(interval * speedScale, maxSpeed)
        local angle = 50 * math.sin(speed * x)
		local eggCFrame = camera.CFrame * offset * CFrame.Angles(0, 0, math.rad(angle))
		egg:SetPrimaryPartCFrame(eggCFrame)
	end
	connection1 = runService.RenderStepped:Connect(onRenderStep)
end
1 Like

It freezes the egg after some time and I can’t really see the speed difference

I made a mistake. Can you try this:

function module:DisplayEgg(eggName)
	local cframe = CFrame.new()
	local egg = workspace['Eggs'][eggName]:Clone()
	egg.Parent = camera
	for _, part in pairs(egg:GetDescendants()) do
		if part:IsA('BasePart') then
			part.CanCollide = false
		end
	end
	local offset = CFrame.new(0,0,-(egg.PrimaryPart.Size.Z * 1.5))
	cframe = offset 
	local x = 8
    local t = 0
    local decay = 10
    local startSpeed = 5
	local function onRenderStep(dt)
		-- Start at 1 and move to 0 over time
		local speed = math.max(-speed / decay * t + startSpeed , 0)
        local angle = 50 * math.sin(t * speed * x)
		local eggCFrame = camera.CFrame * offset * CFrame.Angles(0, 0, math.rad(angle))
		egg:SetPrimaryPartCFrame(eggCFrame)
		
        -- Keep track of time
		t = t + dt
	end
	connection1 = runService.RenderStepped:Connect(onRenderStep)
end
1 Like