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 sxnorthrop · Apr 14, 2018 at 04:15 PM · programmingmathfpingpongsine-wave

Going from 0 to 1 and then back to 0 within a time frame

I can't seem to find a simple example of this anywhere. I don't have much knowledge regarding sine waves but I'm more than certain that's what I need to use for this. I've also tried Mathf.PingPong but I can't seem to get that working correctly either. I honestly don't even know what to do to do this without using some crappy if (value > max) go backwards etc.

There must be an equation, that when passed an elapsed time, can go from min to max then back to min once the elapsed time reaches the desired duration.

 For example with a duration of 1 second:
 
 Elapsed Time of 0
 return value = 0
 
 Elapsed Time of 0.5
 return value = 1
 
 Elapsed Time of 1
 return value = 0
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

1 Reply

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

Answer by Hellium · Apr 14, 2018 at 04:41 PM

 float duration = 1 ; // In seconds
 float maxValue = 1 ;
 float elapsedTime = Time.time - startTime ; // startTime can be equal to 0
 float output = Mathf.PingPong( 2 * elapsedTime / duration, maxValue );

OR, for a smoother ping pong

 float duration = 1 ; // In seconds
 float maxValue = 1 ;
 float elapsedTime = Time.time - startTime ; // startTime can be equal to 0
 float output = elapsedTime * Mathf.PI / duration ;
 output = Mathf.Cos( output + Mathf.PI ) + 1 ;
 output *= maxValue / 2 ;

Desmos : https://www.desmos.com/calculator/gbrkeknld8

Comment
Add comment · Show 4 · 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 sxnorthrop · Apr 14, 2018 at 04:57 PM 0
Share

It works! I substituted Time.time with my own elapsed time, also I moved the max value outside of the pingpong as a multiplier, with 1 as the length, and I seem to be getting the desired result.

Thank you very much! This is my working code:

 public float duration = 1; // In seconds
 public float maxValue = 1;
 public Slider displayValue;
 
 private void Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.B))
     {
         StartCoroutine(Wave(duration));
     }
 }
 
 IEnumerator Wave(float d)
 {
     var elapsedTime = 0f;
 
     while (elapsedTime < d)
     {
         var value = $$anonymous$$athf.PingPong(2 * elapsedTime / duration, 1) * maxValue;
 
         Debug.Log("Value: " + value + "\nElapsed Time: " + elapsedTime);
 
         elapsedTime += Time.deltaTime;
 
         if (displayValue)
             displayValue.value = value;
 
         yield return null;
     }
 }
avatar image Bunny83 · Apr 14, 2018 at 05:07 PM 1
Share

Huh? Why "20"? Why dividing by PI? the angle should be a multiple of PI. This actually creates a proper sine wave that starts at 0 and exactly hits the whole numbers:

 float duration = 1 ; // In seconds
 float maxValue = 1 ;
 float angle = Time.time * 2f * $$anonymous$$athf.PI / duration;
 float output = maxValue * 0.5f* (1f-$$anonymous$$athf.Cos( angle ));

Desmos

edit actually dividing by the "duration" ^^. Since duration and frequency are the reciprocals from each other. t = 1/f and f = 1/t

avatar image Hellium Bunny83 · Apr 14, 2018 at 05:10 PM 0
Share

Well, I fixed my answer, must admit I am tired and I looked for the values by feeling...

avatar image Bunny83 Hellium · Apr 14, 2018 at 05:39 PM 0
Share

No problem. Btw i created a desmos graph for pingpong. The actual implementation of $$anonymous$$athf.PingPong can be found in the reference repository. Repeat is directly above. Clamp can be found further up, though the implemenation should be obvious ^^.

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

85 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 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 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

Multiple Cars not working 1 Answer

Does Mathf.PingPong always have to start at (0,0,0)? 1 Answer

mathf.movetowards not returning right values 1 Answer

Mathf function on GetKey 1 Answer

Ping pong position using lerp 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