Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Sprakle · Sep 20, 2013 at 07:14 AM · mathsine

Math - Changing the frequency of a sine wave in real-time

I need an algorithm that can output a sine wave of varying frequency, with this frequency being changed dynamically. I started with this:

         public void Update()
         {
             time += Time.deltaTime;
             float sine = Mathf.Sin(time * frequency);
         }

The problem occurs when the frequency is changed. Normally as the time variable increases, the sine wave is advanced by that amount, and multiplied by the frequency variable. But when the frequency is reduced while the sine wave is being generated, the wave is temporarily "brought back in time" because the time variable is being multiplied by a smaller number.

So is there an algorithm that can change the frequency of a sine wave in real-time?

Comment
Add comment · Show 4
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 Jamora · Sep 20, 2013 at 07:40 AM 0
Share

Your function does change the frequency of the sine wave. So it seems the problem here isn't actually in changing the frequency, but maintaining the phase of the sine wave at point time.

avatar image Sprakle · Sep 20, 2013 at 07:43 AM 0
Share

That's what I meant to say, but I wasn't farmiliar with the terms.

avatar image robertbu · Sep 20, 2013 at 08:06 AM 0
Share

I think what you are looking for is this:

 float sine = $$anonymous$$athf.Sin(time * frequency + phase); 

 

How to set the phase so that the waves don't advance/retreat will depend on your anchor point. I'd have to see more of your code and/or a better description of the anchor. In order to get a higher frequency, you will have more waves in the same amount of space, so you can only anchor at one point.

avatar image Sprakle · Sep 20, 2013 at 08:18 AM 0
Share

@robertbu

The only other code would be adding the given value to the x/y/z position of a transform. Is that enough to go off of?

1 Reply

· Add your reply
  • Sort: 
avatar image
14
Best Answer

Answer by robertbu · Sep 20, 2013 at 11:02 AM

I made an assumption on how you were going to use this function, but as I read, there is no specification. So here is a bit of code that demonstrates how you can modify the phase to keep the sine in-sync. Create an game object at the origin and attach the following script. While the script is running, change the frequency in the inspector. The game object will move back and forth. When the frequency is changed, the game object will not jump.

 using UnityEngine;
 using System.Collections;
 
 public class SinePhase : MonoBehaviour {
     
     public float amplitude = 2.0f;
     public float frequency = 0.5f;
     private float _frequency;
     private float phase = 0.0f;
     private Transform trans;
 
     void Start () {
         _frequency = frequency;    
         trans = transform;
     }
     
     void Update () {
         if (frequency != _frequency) 
             CalcNewFreq();
         
         Vector3 v3 = trans.position;
         v3.x = Mathf.Sin (Time.time * _frequency + phase) * amplitude;
         trans.position = v3;
     }
     
     void CalcNewFreq() {
         float curr = (Time.time * _frequency + phase) % (2.0f * Mathf.PI);
         float next = (Time.time * frequency) % (2.0f * Mathf.PI);
         phase = curr - next;
         _frequency = frequency;
     }
 }
Comment
Add comment · Show 7 · 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 Sprakle · Sep 22, 2013 at 04:38 AM 0
Share

That worked perfectly. Thanks.

avatar image mikeyboi49 · May 21, 2014 at 01:33 AM 0
Share

although this post was since last year, I'd just like to give a thumbs up, as this is perfect for what I'm working on right now. Cheers to you, 'bert!

avatar image keenanwoodall · Jul 02, 2015 at 05:27 PM 0
Share

I'm here a bit late, but how would I add an offset to the wave without messing up the frequency again? I'm looping through a mesh's vertices and want it to make a wave (like kelp in the ocean) so I want to offset the wave's calculation based on the vertice's y-height.

avatar image churi24 · Nov 14, 2017 at 10:16 PM 0
Share

Thank you very much!! I use this with Cinemachine to improve the noise for walk, run and idle.

avatar image NetherChef · Nov 04, 2018 at 06:21 PM 0
Share

I would love to find out how this works someday, but for now, this is nothing short of amazing. Thank you very much for sharing.

Show more comments

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

24 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

Related Questions

Top down wasd movement dependent on rotation of the character - Mathematical problem!!! 2 Answers

Sine Wave Without the SIne Function? 3 Answers

Trying to obtain sine/cosine by angles giving me imprecise values 1 Answer

I can't find cossine. 1 Answer

Oscillating variable 6 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