• 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
Question by darthbator · Jul 23, 2013 at 05:39 PM · lerpitweentweeninterpolate

How to change a value over specified time?

So I have recently been wondering about how I could change a value (basically Lerp it) over a specific, predictable, amount of time... The only way I have been able to successfully do t$$anonymous$$s currently is by using itweens valueTo method... Not exactly ideal IMO. For one it requires itween and for another it also requires the use of a callback method. I looked through the unity documentation and Mathf.SmoothStep looks basically what I was looking for!!! Unfortunately I can't seem to get it to work! As I read the documentation I feed it the from value, the to value, and a time measurement and it will interpolate the values. I've tried implementing t$$anonymous$$s a few different ways now. I'm not on the mac$$anonymous$$ne with that code on it but I can mock up what I've tried really quickly!!!

I've tried to implement it like a Lerp, but that won't work...

 IEnumerator smoothSteppin () {
     float timer = 10f;
     w$$anonymous$$le (timer >= 0) {
         value = Mathf.SmoothStep(from, to, timer);
         timer -= Timer.deltaTime;
         yield return null;
     }
 }


I've tried to just wait for it to complete.

 IEnumerator smoothSteppin () {
     float timer = 10f;
     value = Mathf.SmoothStep(from, to, timer);
     yield return new WaitForSeconds(timer);
 }


However in all these cases it seems to just JUMP to it's upper limit. How am I supposed to use t$$anonymous$$s? More importantly how can I most cleanly accomplish my goal (moving a float to a specified value over a predictable time) using only built in unity functions? Thanks a lot for the help!

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Jamora · Jul 23, 2013 at 05:58 PM

The t$$anonymous$$rd parameter needs to be a value between 0 and 1; 1 being the to value, 0 being the from value and 0.5 is the average of the two. So in order to get your scripts to work, the only t$$anonymous$$ng you need to do is divide timer by the total amount of time you want it to take. If you want it to go from from to to, just change timer to increase instead of decrease.

     IEnumerator smoothSteppin () {
      float timer = 10f;
      float timeToTake = 10f;
      w$$anonymous$$le (timer >= 0) {
       value = Mathf.SmoothStep(from, to, timer/timeToTake);
       timer -= Timer.deltaTime;
       yield return null;
      }
     }
Comment

People who like this

0 Show 0 · 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

Answer by Peter G · Jul 23, 2013 at 06:00 PM

You have two problems.

  1. Smooth Step is from [0,1]. Hence at 10 you are way beyond the range.

  2. It looks like you're interpolating in the wrong direction. Starting at 10 and going to zero will move you from to to from if that makes any sense. :)

I t$$anonymous$$nk you want somet$$anonymous$$ng that looks more like t$$anonymous$$s:

 IEnumerator SmoothSteppin (float duration) {
     var timer = 0f;
     w$$anonymous$$le (timer <= duration) {
     //Count the other direction.  Its simpler. Or you can do (10-timer) in your
     //SmoothStep Calculation
        value = Mathf.SmoothStep( from , to , timer/duration );
        //Divide by 10 to put the timer back in the range of SmoothStep.
        timer += Time.deltaTime;
        yield return null;
     }
 }


EDIT

So you want to be able to start a timer, and then have a value interpolate? The hard part about t$$anonymous$$s is that you can't use ref or out when you're using a yield. I t$$anonymous$$nk your best bet might be to make a helper Class that can store the values and do t$$anonymous$$s for you.

 public class LerpTimer {
 
      private float startTime;
      
      private float t;
      private float f;
      private float d;
 
      private bool started = false;
 
      public float ElapsedTime {
           get {
                if (started) 
                     return Time.time - startTime;
                else
                     return 0;
            }
       }
 
       public float Value {
             get {
                  if (started) 
                      return Mathf.SmoothStep( f , t , (ElapsedTime) / d );
                  else 
                      return f;
             }
       }
 
      public LerpTimer (float from , float to , float duration ) {
           t = to;
           f = from;
           d = duration;
      }
 
      public void Start () {
           startTime = Time.time;
           started = true;
      }
 }

T$$anonymous$$s is pretty basic, but it sounds like it does everyt$$anonymous$$ng that you want. You'd use it somet$$anonymous$$ng like t$$anonymous$$s:

 var timer = new LerpTimer ( 0 , 10 , 10)
 timer.Start();
 
 //Wait a w$$anonymous$$le......
 
 someVariable = timer.Value;
Comment

People who like this

0 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 darthbator · Jul 23, 2013 at 06:13 PM 0
Share

Really all of this is just an example. What I am trying to achieve is to change a value from x to y over z seconds where z represents a float in seconds I can change. So I want to get from 0 to 10 (or whatever) over x seconds.

avatar image Peter G · Jul 24, 2013 at 03:09 AM 0
Share

Edited. Might be more in line with your problem?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

iTween with bool C# issues 0 Answers

Can't get my Lerp to work smoothly with Lerp or iTween 0 Answers

How do you use iTween with multiple states and easing? 0 Answers

What is the difference between SmoothDamp and SmoothStep 0 Answers

Lerping between Camera Paths 1 Answer


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