Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Greg-Bassett · Jul 30, 2013 at 10:07 PM · ponginoutping

Mathf.PingPong with ease in and out?

I have the following code in Update() which counts from 0 to 100 and back to 0, but I want to make the value start slowly, build up to speed as it increases, then slow as it reaches 100, and then speed up as it starts back to 0, and again slow as it reaches 0.

I need to use the value to ease in and out from 0 to 100

Debug.Log(Mathf.PingPong(Time.time * 10, 100));

I have looked at lots of the Mathf functions but no joy... anyone able to help?

Comment
Add comment · Show 2
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 Greg-Bassett · Jul 30, 2013 at 11:05 PM -1
Share

Neither examples are giving me what I want? Hermite is closet, but the ease in and out is so slight it is hardly noticeable, is there a way to define the ease in and out amount, the effect I want is like a bouncing ball.

In fact what I really need is my value to go 0 to 100 the first time, then 0 to 80 the next time, and then 0 to 60 etc, etc...

avatar image Bunny83 · Jul 30, 2013 at 11:11 PM 0
Share

@greg-bassett: Don't post comments as answers. Answers should answer the question. What you describe in your comment is not really what you said in your question. You said you want to ease in and ease out. a bouncing ball just eases at one end. At the bottom it actually has the highest speed when it returns.

It's still not clear what you really want. If you want to reduce the max value each cycle it needs some reference since Time.time just keeps growing. If you want to simulate physics, why don't you use physics? At least what you describe is not really a usual interpolation function. Or can you draw the graph of your position function?

2 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Eric5h5 · Jul 30, 2013 at 10:32 PM

 Mathf.SmoothStep(0, 100, Mathf.PingPong(Time.time/10, 1))
Comment
Add comment · Show 2 · 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 Bunny83 · Jul 30, 2013 at 10:36 PM 0
Share

Just had a look and SmoothStep is just a hermit polynom:

 public static float SmoothStep(float from, float to, float t)
 {
     t = $$anonymous$$athf.Clamp01(t);
     t = -2f * t * t * t + 3f * t * t;
     return to * t + from * (1f - t);
 }
avatar image Eric5h5 · Jul 30, 2013 at 11:03 PM 0
Share

Yep, I used to write code which did that before they implemented SmoothStep. A $$anonymous$$or thing but it's kind of nice to have it built-in, since I tend to use hermite interpolation mostly.

avatar image
6

Answer by Bunny83 · Jul 30, 2013 at 10:20 PM

Well, there are many ways to do this. One is to use Mathf.Cos along with Math.PingPong.

Something like that:

     // C#
     (Mathf.Sin(Mathf.PingPong(Time.time*Mathf.PI, Mathf.PI))+1)*0.5f*100f

Another way is a Hermite interpolation:

     float Hermite(float t)
     {
         return -t*t*t*2f + t*t*3f;
     }
     
     Hermite(Mathf.PingPong(Time.time,1.0f))*100f
Comment
Add comment · Show 1 · 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 Bunny83 · Jul 30, 2013 at 10:28 PM 1
Share

So basically the first solution uses the cosinus (the blue graph) between 0 and PI and ping pongs this part.

The second solution uses a hermite interpolation:

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

18 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

Related Questions

How to fade in GUI text. 2 Answers

Getting In And Out Of Cars With Jcar 0 Answers

Fade In and out when level is loaded 2 Answers

Fade 3d text with lerp? 1 Answer

Easy fade in/out on level load/end? 2 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