How can i use Math.Sin to move motors in a wave pattern?

Im making some new beams and i need some sine effects but i dont know how to do this i hear i needed to use Math.sine but in very unsure in how to implement this into a script im trying to achieve sort of a wave pattern With motors Any help is appreciated.

This?

local Number = 1
local Height = 1
local Speed = 10

local Part = Instance.new("Part")
Part.Anchored = true
Part.Parent = workspace

while wait() do
	local MathSin = Height * math.sin(Number / Speed)
	Part.CFrame = CFrame.new(0, MathSin, 0)
	Number += 1
end
2 Likes

Generally people love to stay away from complex trig problems :wink:

Anyways, if you could elaborate a bit more on what you are trying to accomplish? Just a sine pattern?
image

You can accomplish this just by messing around with degrees if you want

while true do
   for i=1, 360 do
      print(math.sin(math.rad(i)))
      wait()
   end
end

You can mess around with code like this (which would move a part around)

workspace.Part.Position = workspace.Part.Position + Vector3.new(0, math.sin(math.rad(i))*.5, 0)

I would just suggest testing things out on your own as trig can be one of those things that you just need to troubleshoot

2 Likes

how can i make it output a max of 125 and a minimum of 45

Your parent formula is y = sin(x)

In order to increase the range of the function (max/min) we multiply on the outside
y = 40sin(x)
(The 40 is 80 / 2)

after that we want to change where it starts so we add
y = 40sin(x) + 85
(85 since 85 - 40 = 45 and 85 + 40 = 125)

1 Like

so what would a script with this look like?

local x = 123 -- variable can be i if used in a loop or something
math.sin(math.deg(x))*40 + 85
1 Like

It does not really work for me i will show you a video of what happens

Here is my script

for i,v in pairs(workspace.Ulights:GetChildren()) do
spawn(function()
while true do
local x = 123 – variable can be i if used in a loop or something
v.LightingController.Speed:Invoke(1)

  	v.LightingController.Tilt:Invoke(math.sin(math.deg(x))*40 + 85)
  	wait(.1)
  end

end)
wait(.5)
end

local configSpeed = 0.1 -- change this to make it faster/slower
local configMin = 45
local configMax = 125

local evHeartbeat = game:GetService("RunService").Heartbeat
local progress = 0
evHeartbeat:Connect(function(timeDelta)
	progress+=timeDelta*configSpeed
	
	local tilt = (math.sin(progress)+1) * (configMax-configMin) / 2 + configMin
	for i,v in pairs(workspace.Ulights:GetChildren()) do
		--[[
			Now you set your tilt here
			I don't get what's with those invokes
			Sine waves are already smooth and this code runs on every frame
		]]
	end
end)
1 Like

thx for help ima try it now i hope it works

nope still broken it wont work rly it does sort of the same thing as before

What do the invokes actually do? Do they call tweens, or what?

This problem is linked to your implementation. Unless you are using someone else’s script, the way I’d do this is have each light contain a ModuleScript containing a function which returns the position on each call. By default, it is a function that always returns 0.

Right now, I believe the issue lies with the tweens. There is no reason to tween something that’s already smooth. What probably happens is the sine wave is applied correctly, but the tween smoothens it out from a range of 45-125 to something like 82.5 - 87.5, making it seem like they aren’t moving.

1 Like

If you are using a 6Dmotor you could just set the desired angle or the current angle to the result of the math.sin operator?

1 Like

I made a simple example of what I meant. It doesn’t use motors, but the principle is very similar.
The actual code is contained inside the Module ModuleScript, while the script assigns an interpolation function.
test.rbxm (4.6 KB)

3 Likes

You can also use cos and tan to geenrate simular wave patterns.
See this plugin by CloneTrooper to better understand then Wave Generator - Roblox

Also Tweens and TweenService could fit your needs better. TweenService | Documentation - Roblox Creator Hub Here is a plugin to help you with tweensequences TweenSequence Editor - Roblox

2 Likes

Edited the code a bit to fit your use-case better. Now the function returns a Vector2, with X being the horizontal and Y being the vertical rotation. The previous one worked as if it was on a gimbal.
test2.rbxm (4.6 KB)

2 Likes

Ok i know its late but i got it to work but they all move at the same it should be L1 will move then L2 etc it goes on to L8 how do i do that?

i would highly reccomend you to not use roblox’s physics for that sorta stuff.
but as you can read below, math.sin returns the value on a sine wave.
By putting this into a for loop with value x in radians increasing to 2*pi, you can make visualize a sine wave by increasing the X position by one and having Y be the sine of x. (Vector3.new(X,math.sin(X),0))
The radius of the wave can be adjusted by multiplying the sine with the radius.
To make it make a circle, all you’ll need to do is to have X be the cosine of x and Y the sine of x.

1 Like

ok i made this script but it not work

local configSpeed = 0.1 -- change this to make it faster/slower
local configMin = 45
local configMax = 125

local evHeartbeat = game:GetService("RunService").Heartbeat
local progress = 0
evHeartbeat:Connect(function(timeDelta)
	progress+=timeDelta*configSpeed
local nextmove = 1
	local tilt = (math.sin(progress)+1) * (configMax-configMin) / 2 + configMin
	for i,v in pairs(workspace.LLights:GetChildren()) do
		if v.Name == ("L"+nextmove) then
		v.Motors.Tilt.DesiredAngle = math.rad(tilt)
			end
			if nextmove == 8 then
				nextmove = 1
			else
				
				
				nextmove = nextmove + 1
				end
			end

end)

What happens when you try this script?

I think a more versatile implementation would involve making use of the positions of each light so that you could use a formula like:

θ = Asin( k.r - ω t) + C

Where r is the position of the light and k is the wave vector, which is inversely proportional to the wavelength of the effect.

1 Like