• 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 this 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 this a few different ways now. I'm not on the machine 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;
     while (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 this? 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

  • Sort: 
avatar image

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

The third 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 thing 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;
      while (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 think you want something that looks more like this:

 IEnumerator SmoothSteppin (float duration) {
     var timer = 0f;
     while (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 this is that you can't use ref or out when you're using a yield. I think your best bet might be to make a helper Class that can store the values and do this 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;
      }
 }

This is pretty basic, but it sounds like it does everything that you want. You'd use it something like this:

 var timer = new LerpTimer ( 0 , 10 , 10)
 timer.Start();
 
 //Wait a while......
 
 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?

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

How to lerp with a changing "from" value? 2 Answers

How to tween a variable using iTween 1 Answer

Help setting up a public Ease Type variable in LeanTween 1 Answer

Get positions at equal intervals between two Vector3 3 Answers

Asynchronously and smoothly move a 2D player character? 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