How would I run code when a cosine wave reaches a specific point?

Hello. I am making a realistic-ish FPS framework for a project. Right now it’s going absolutely fantastic, but there’s one thing I can’t figure out.

I want to time a footstep sound every time the camera bobbing cosine wave reaches the bottom point, or even the mid point. All the camera bob does is move up and down, and side to side. Is there even a way to do this?

Here are some code samples:

This is the function used for creating the cosine waves

function getBobbingcos(addition,speed,modifier)
	return math.cos(tick()*addition*speed)*modifier
end

This is where the function is called [It is located in a RunService.Renderstepped event]

local camSway = Vector3.new(getBobbing(4,speed,modifier),getBobbingcos(4,speed*2,modifier*1.5),0)

It’s in the middle of the Vector3

getBobbingcos(4,speed*2,modifier*1.5)

Any help is appreciated, and if you need me to further clarify anything, please do let me know. Thank you!

Forgot to attach a video.

You can use a range to trigger the event as sound generally fits within a couple milliseconds of the feet actually touching the floor (Usually after). In this case your looking for when the cos reaches -1*modifier*1.5. You can use Desmos to visualize how the cos works.

Sorry but this doesn’t really help much. Not saying its your fault but it’s rather my fault. I’m not that great at math, especially when it comes to math in coding. Thanks for responding though.


Cos is easy to visualize and usually looks like this. Since you are looking for the lower value the if statement would look like this

if getBobbingcos(4,speed*2,modifier*1.5) <= (-1*modifier*1.5)+rangeVal then
--Code Here
end

What would rangeVal be?

Ignore this, its only here to reach the character limit

Tested this and it sorta worked, but I instead figured out another method that worked great inspired by this.

I am instead checking if the Y level of the already existing camera bobbing is below a specific level, and if it is, run a piece of code, but add a debounce that only goes back to false if the y level is above 0, thus preventing the sound from playing hundreds of times at once.

Thanks for your help, stranger!

How do you make the bobbing for the gun? I can’t find any posts, sorry if this isnt relevant or I couldn’t help. Just kinda desperate for an explanation