- Home /
getting the lowest point of a sine wave in javascript
Hi! you'll see, i've got this script from the wiki in javascript:
waveslice = 0.0;
if (Mathf.Abs(inputX) == 0 && Mathf.Abs(inputY) == 0) {
timer = 0.0;
}
else {
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2) {
timer = timer - (Mathf.PI * 2);
}
}
if (waveslice != 0) {
translateChange = waveslice * bobbingAmount;
totalAxes = Mathf.Abs(inputX) + Mathf.Abs(inputY);
totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0);
translateChange = totalAxes * translateChange;
myCamera.transform.localPosition.y = midpoint + translateChange;
}
else {
myCamera.transform.localPosition.y = midpoint;
}
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
Answer by luminance · Sep 24, 2015 at 10:37 PM
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
Answer by Kiwasi · Sep 25, 2015 at 01:28 AM
Why do you care about the sine wave value. The sine curve is entirely predictable. It will reach its lowest point when the timer is equal to 1.5 * PI
So why not try this?
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 1.5) {
timer = timer - (Mathf.PI * 2);
// Play the audio here
}
Answer by LudwigVK · Sep 25, 2015 at 01:50 AM
Thnks very much! pointed me in the right direction, i ended up doing this:
if(waveslice <= minHeight && stepSwitch == false)
{
Instantiate(stepPrefab, transform.position, transform.rotation);
stepSwitch = true;
}
if (waveslice >= 0)
{
stepSwitch = false;
}
i used Debug.Log to check which was the min point, which is 0.999 or something close, so "minHeight" is set up to 0.8 which works just fine
Your answer
Follow this Question
Related Questions
Unity Exponentiation question. 1 Answer
Move speed not working properly 1 Answer
[JS]Rotate object y axis to mouse position 1 Answer
xp math equation help 1 Answer
calculate terms in runtime 3 Answers