i’m having a hard time trying to get the lowest point of the sine. What i wan’t to to do is to check if the “myCamera” object is currently on the lowest point of the sine curve, if true, play a sound.
I tried checking the transform.position.y of the object but this is inacurate since the terrain is not always flat, also localPosition fails at it because my character “can crouch” getting the myCamera object to move in a lower position.
Any suggestion? Thnks :b
You could check if the sine value is -1, the ‘lowest’ point of a sine wave:
waveslice = Mathf.Sin(timer);
if(Mathf.Approximately(waveslice, -1)) // Using Mathf.Approximately here because of floating point inaccuracy with == comparisons
{
// Do something
}
But you probably don’t want to do this, because there’s no guarantee that the sine wave will ever reach exactly -1 depending on your bobbingSpeed variable.
Since you’re keeping track of the timer, you could probably do something like…
// Mathf.Sin(Math.PI * 1.5) equals -1f
// This checks if the timer is about to become greater than that threshold, ensuring that the sound only plays once per loop.
if(timer < Math.PI * 1.5 && timer + bobbingSpeed > Math.PI * 1.5)
{
// Do something
}
timer = timer + bobbingSpeed