Making a sine wave between 2 parts

I want to make a sine wave between 2 parts. The issue is… I don’t know how to do this… Here is my failed attempt. If you need any more info just ask. Thanks.

local a = workspace.Test.a
local b = workspace.Test.b
local c = workspace.Test.c
local t = workspace.Test.Handle

local Start = a.Position
local End = b.Position

local SX = Start.X
local SZ = Start.Z

local EX = End.X
local EZ = End.Z

local INCREMENT = (SX-SZ)/3000

print(INCREMENT)

local angle = 0

local I = 0

t.CFrame = CFrame.new(Start, End)

print((Start-End).Magnitude)
-- ((Start-End).Magnitude)*7

while I < (Start-End).Magnitude*9.375 do
	I = I+1
	
	local Mas = math.sin(angle)
	
	t.Position = t.Position+Vector3.new(Mas, 0, 0.1)
	
	angle = angle + INCREMENT
	
	print(I)
	
	if I/2 == math.floor(I/2) then
		wait()
	end
	
	if angle >= (math.pi * 2) then
		angle = 0
	end
	
	--if I == 1000 then print("f") break end
end
1 Like

Have you tried using tween service yet?

I’m assuming you want to move a part from Position 1 to Position 2. Im also assuming that you want the sine to control the height. This means that the X/Z interpolation will be linear, so we just need to calculate the Y offset from a straight line between pos1 and pos2.

local part1 = workspace.part1
local part2 = workspace.part2
local movementPart = workspace.movementPart

local amplitude = 20 -- height of your wave

movementPart.CFrame = part1.CFrame

-- one cycle of a sin wave exists between 0 and 2pi
local cycleLength = 2*math.pi

-- iterate over one cycle
for i=0, cycleLength, math.pi/100 do -- or some other arbitrary increment
   local alpha = i/cycleLength 
   local yOffset = Vector3.new(0, math.sin(i)*amplitude, 0)
   movementPart.CFrame = part1.CFrame:lerp(part2.CFrame, alpha) + yOffset
   wait() 
   -- replace this whole operation some kind of heartbeat connection to appease dev forum gods
end

This results in this behavior: https://gyazo.com/882ff2d6bb40d54fab71ff471eb7f8ee
Because CFrame:lerp is doing all the hard work for us, it will work in any orientation

3 Likes

This is sorta what I want. But I want the x and z-axis, it should also just tween along the Y-axis but I can do that. Technically it’s just one of the axis because it will tween towards the goal and go in a sine wave pattern when on the axis that isn’t forward (X or Z). And also I want it to perfectly stop at the second part, but if you can’t do that just make it as close as possible and I’ll just tween it from there. If you need any more info I can give you some. I kinda confused myself when writing this…

It perfectly stops at the second part, my gif maker just had a time limit.

You can mix and match along any axis, just replace the code in yOffset to one or more of the other axes

Thankyou, I’ll test it soon.

30chars

Is there a way to draw the trail instantly, or very quickly. Quicker than heartbeat?

Just do the for loop without any yielding. Larger increments in the for loop will make the code execute faster, but you will have a less accurate (or more holey?) sine wave. e.g for i=0, 2*math.pi, 0.5