Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use Sinus and Cosinus for smooth movements?

Asked by 5 years ago
Edited 5 years ago

I want to make the parent of the script move smoothly up and down continuously. I know I probably need to use sin or cos but I don't know how to exactly use them. For now, I made my Part rotate forever.

while wait(0.001) do
script.Parent.Orientation = Vector3.new(script.Parent.Orientation.X,script.Parent.Orientation.Y + 0.25, script.Parent.Orientation.Z)
end

That part is this Sell sign And my question is: How to do it? And if maybe there is other easier way to do it, please tell me how.

2
sine(tick()) or cos(tick()); remember: sin = opposite/hypotenuse and cosin = adjacent/hypotenuse. Fifkee 2017 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Please accept this answer if it solved your problem.

Question

I want to make the parent of the script move smoothly up and down continuously.

Solution

You can use the math function sin which will return a number from -1 to 1 depending on what number is entered.

The sin function is a function part of the math library. Therefore: math.sin(x) The sin function in lua's math library takes in *radians. *

The sine wave itself.

(This is going to be a broad explanation)

A general formula can be used for constructing sine waves: sin(ta + b) * c

Where a can represent the "speed" of the wave for a more broad term; b the phase which is the shift along the x axis; c is the amplitude which affects the range of the function; and t which is time.

sin(5t + 2) * 2 would generate a sine wave shifted 2 units to the left, a range of [-2, 2] and oscillating 5 times faster.

The Sin function applied in an example.

In this case we want to make a part move up and down continuously. What should we do? We can use the above mentioned sin wave.

Sidenote: We will be using tick() as t (time)

First, we will want to define reference to the service RunService. RunService has a signal Heartbeat which essentially fires every frame. Making it incredibly fast and a useful tool to make the time between the change in position of the part very small. (look smoother)

Then, we will store the original position of the part so we can offset the position later on the y axis so it may float up and down. We will need to connect a function to the Heartbeat signal.

Inside our connected function we will define y; which will be the offset on the y axis of our original position using the above mentioned formula.

If we want it to oscillate slowly we would make the number multiplying t by smaller and vice versa. Also if we want to increase how high and low the part would go we would multiply the returned value from sin by some number where the higher it is the higher the range is scaled. (sin(x) * 5 would yield a range of [-5, 5])

Finally, we update the position using our offset.

In this example code the part will oscillate 10 times slower than it would normally and would have a range of 1.5 meaning it would go up 1.5 studs and then down 1.5 studs between one cycle. [-1.5, 1.5]

local runService = game:GetService("RunService")

local part = ... --// you will need to define reference to the part. 
local startingPosition = part.Position

local function float()
    local t = tick()
    local y = math.sin(t * .1) * 1.5

    part.CFrame = startingPosition + Vector3.new(0, y, 0)
end

runService.Heartbeat:Connect(float)

Further Information:

For more information on sine waves and radians; as well some of the above mentioned, I would check these out:

https://en.wikipedia.org/wiki/Sine_wave

https://en.wikipedia.org/wiki/Radian

This redirects to a cached version of the documentation v https://developer.roblox.com/en-us/api-reference/event/RunService/Heartbeat

https://developer.roblox.com/en-us/api-reference/datatype/CFrame

0
sir he asked how to apply sin to his code not for a whole calculus lesson jesus christ Fifkee 2017 — 5y
1
Actually, this answer helped me so much and showed me even more useful stuff and I appreciate it! Rafii2198 56 — 5y
Ad

Answer this question