My sine/cos functions are being inconsistent and weird

I am animating a character using solely math, that is to say using sine and cos functions to give it movements. However, I have run into a problem in that for some reason my sine and cos waves are being really inconsistent, sometimes when I load the game they are super exaggerated and sometimes they are barely visible. Not to mention, my sine waves seem to only go positive and back to its original position, never dipping into negative:

This is the formula I have for my functions, from there I just apply it to the cframes of the attachments in a heartbeat function

	local sine = math.cos(tick() * 2) * .2 -- speed, strength
	local sineCircle = math.cos(tick() * formulaC) * .2 

    nail.C1 = nail.C1 * CFrame.Angles(math.rad(-sine/2),math.rad(-sine),0)

Why aren’t you using the math.sin function?

1 Like

You are using the rotation in object space formula so it will change direction depending on the previous c1 and perhaps even .Transform animation property.

Consider or try using rotation in world space axis if you want it more consistent.

The variable is named “sine”, but you’re using cos which is just sin but offset by about a quarter of a period (I believe).

This possibly could be why you are having this unexpected behavior. Also, from my experience you may want to change BOTH C1 and .C0 values.

Hi, sorry I should have made this more clear but I am using cos even though it’s called sine, it was previously sine but i was having the same problem so it doesn’t matter anyways.

How would I go about doing this? I’m not experienced with cframes,

local sine = math.cos(tick() * 2) * .2 -- speed, strength
local sineCircle = math.cos(tick() * formulaC) * .2 

nail.C1 = CFrame.Angles(math.rad(-sine/2),math.rad(-sine),0) * nail.C1

Try this

Use a time of measurement different from tick(). There’s no reason your wave should be in the range between -0.1 and 1 without something having gone wrong like the frame rate limit messing with your function output

Actually your sine variable shouldn’t even above 0.2 because that’s the radius you defined. Something else has gone wrong or you’re expecting a different result from a function you wrote incorrectly

Whats wrong with tick? I need to use tick as this function updates evey frame to make it animate, also yes it is inbetween .2, I just used 1 as an example in the photo above

There’s nothing wrong with tick(), I was just concerned about your issues coincidentally lining up with symptoms of a fixed interval. For example, running a cosine function every 6.28 milliseconds would result in the same output every time

Now that we know the function output isn’t what you wrote, make sure that you are converting units properly. Remember that 2pi is 1 full loop around the circle. In the current function, 1 millisecond is 32% of the circle/loop for your animation. You will likely end up writing a ratio, something like 1/(2pi) to represent 1 second per loop

Well, I was able to find the answer, but by complete accident, all I did was add a lerp function and it solved all my problems

neck.Parent.Torso.Neck.C1 = neck.Parent.Torso.Neck.C1:lerp(bobble, .30)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.