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 darkhog · Sep 20, 2013 at 12:56 PM · timechangelinearover

How to change value of variable over period of time?

I have some variables that I'd like to linearly twine from one value to another over specific amount of time. How do I do that?

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

2 Replies

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

Answer by Moohasha · Sep 20, 2013 at 01:30 PM

Use the Lerp function in Mathf: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html

You might also look at doing this in a Coroutine instead of Update so that once you reach your target value you can just exit.

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 darkhog · Sep 20, 2013 at 02:10 PM 0
Share

Hm, thanks!

If I understand it correctly, when I use

 transform.position = Vector3($$anonymous$$athf.Lerp($$anonymous$$imum, maximum, Time.time*5), 0, 0);

it'll change over 5 seconds?

avatar image Moohasha · Sep 20, 2013 at 03:50 PM 0
Share

Quite the opposite. You are multiplying the time value by 5, meaning that time is increasing 5 times faster, not slower. You would need to divide by 5 for it to take 5 seconds.

Also, Time.time is the time since the game started. Since the t value in Lerp needs to be in the range [0, 1], this would only work for the first second of the game. I suggest using Time.deltaTime each frame and maintaining your own t value.

 float t = 0;
 
 void Update()
 {
   t += Time.deltaTime / duration;
   value = $$anonymous$$athf.Lerp($$anonymous$$imum, maximum, t);
 }
avatar image darkhog · Sep 20, 2013 at 04:07 PM 0
Share

Thanks! Selecting as best answer!

avatar image StarStorms21 · Oct 23, 2019 at 06:26 PM 0
Share

Hey sorry if this is kinda necroposting, i was wondering if you can get the 'time remaining' (like the time until it reaches the required result) from this correct answer to the question.

avatar image
0

Answer by goo-muffin · Sep 20, 2013 at 01:02 PM

 var value :float;
 var periodLength :int; //Seconds
 var amount :float; //Amount to add
 function Update()
 {
    value = Mathf.Floor(Time.time * periodLength) * amount;
 }

This is untested. Just try out. If this shouldn't work, then just look for Coroutine at the Unity Script Reference.

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 darkhog · Sep 20, 2013 at 01:06 PM 0
Share

Hm, would make sense to make periodLength a float so I could use fractions of seconds as well.

And not necessarily adding. I need linear twine from one value to other over specific amount of time. That means also FRO$$anonymous$$ 30 TO 10 over 6.8sec.

I don't want to change thing BY some amount, just FRO$$anonymous$$ value TO value. Sorry if I came off offensive but just had argument with my in-laws, so...

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

Slowly making the player go faster. 0 Answers

I make a camera that change position every X seconds but i need different waiting time for each position that take. 0 Answers

referring to a particular material 0 Answers

Time of day script not working 2 Answers

Time elapsed between variable change? 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