Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LudwigVK · Sep 24, 2015 at 09:02 PM · javascriptmathsine-wave

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
     } 
  
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges