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
1
Question by ronronmx · Feb 06, 2011 at 07:00 AM · lerpmath

How do i add "A" to "B" over a period of time?

So what I'm trying to do is add a pre-defined value - let's say 15 - to a current value, at a rate of let's say 0.2F per frame...and not all at once.

For example:

private float boostCharge = 0.0F public float maxBoost = 50.0F; public float boostAdd = 15.0F; public int duration = 5;

void Start () { boostCharge = maxBoost; }

void Update () { if (boostIsOn) { boostCharge--; boostCharge = Mathf.Clamp (boostCharge, 0.0F, maxBoost); } if (hitPowerUp) { boostCharge += boostAdd; // That's where I'm stuck...I want to add boostAdd to boostCharge at the rate of let's say 0.2F on every frame, instead of all at once. } }

I hope my example above is clear enough. What's the best way to achieve this? Thanks for your help!

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
1

Answer by Jesse Anders · Feb 06, 2011 at 07:06 AM

Typically you'd want to make things framerate-independent by using Time.deltaTime; as it is now, the speed of the decrease in boostCharge (and the increase, even if you scale it by a fixed amount) will depend on the framerate, which probably isn't what you want.

As for your question, a reasonable approach would be to have a boost decrease rate and a boost increase rate in units per second. Then, you would decrease like this:

boostCharge -= boostDecreaseRate * Time.deltaTime;

And increase like this:

boostCharge += boostIncreaseRate * Time.deltaTime;

You can then clamp the results or make other adjustments as needed.

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 ronronmx · Feb 07, 2011 at 04:18 AM 0
Share

Jesse thanks for your help. I think my question was a little bit confusing, as I'm already doing what you suggested, and after thinking about my problem more, I realized that what I thought I wanted to do is actually not possible, so what I'm now doing is adding 0.1F on every frame until a == b+c.

avatar image ronronmx · Feb 07, 2011 at 04:21 AM 0
Share

btw, quick question...if I understand correctly, then: boostCharge -= boostDecreaseRate; // is framerate dependent, and: boostCharge -= boostDecreaseRate * Time.deltaTime; // is framerate-independent. Is this correct?

avatar image Jesse Anders · Feb 07, 2011 at 06:34 AM 0
Share

Yeah, that's correct.

avatar image ronronmx · Feb 08, 2011 at 06:04 AM 0
Share

Thanks for your help!

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

No one has followed this question yet.

Related Questions

Mathf.Lerp is just jumping to maximum, no 5.0 interpolation. 1 Answer

Constant total time over multiple Lerp functions 4 Answers

How to get inverse of a lerp function 1 Answer

How to lerp with a changing "from" value? 1 Answer

Calculating jump while rotating around pivot 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