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
0
Question by inglipX · Apr 05, 2013 at 09:50 PM · timehealth

Increase health smoothly not instantly?

I have a soda drinking script where if i press U it allows me to only once add 8 to my current health (60%). It works good but when i press U it instantly goes to 68. How can i make it smooth like it counts up 60 61 62 63 64.....68???

Here's the script:

 var isReady : boolean = true;
 var isPlayed : boolean;
 var healthGiven : boolean;
 var sodaDrinkAudio : AudioClip;
 var playerMovementScript : PlayerMovementScript;
 var health : AdvancedHealth;
 var sodaHealth : int = 9;
 
 
 
 
 function Update () {
 
     if(Input.GetKeyDown("u") && isReady && !healthGiven && !isPlayed && playerMovementScript.currentGun == gameObject)
     {
     healthGiven = true;
     isPlayed = true;
     
     
     if(healthGiven && isPlayed)
     {
     isReady = false;
     giveHealthSoda();
     audio.PlayOneShot(sodaDrinkAudio);
     
     
     }
 }
 }
     
     
 
     
 
     
 function giveHealthSoda () {
     
     health.maxHealth += sodaHealth;
     
     
     
     
     
 
 }
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 dorpeleg · Apr 05, 2013 at 10:03 PM 0
Share

Do you want it to slowly increase while you hold U, or slowly increase after you click U once?

avatar image inglipX · Apr 05, 2013 at 10:34 PM 0
Share

slowly after i press it once

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by dorpeleg · Apr 05, 2013 at 10:45 PM

Use Mathf.Lerp

Like so:

 health.maxHealth += Mathf.Lerp(0, sodaHealth, Time.time);


Should work

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 inglipX · Apr 05, 2013 at 11:21 PM 0
Share

didn't work :(

but i did this to fix it actually

 function giveHealthSoda () {
     
     yield WaitForSeconds(5);
     health.maxHealth += 1;
     yield WaitForSeconds(.08);
     health.maxHealth += 1;
     yield WaitForSeconds(.08);
     health.maxHealth += 1;
     yield WaitForSeconds(.08);
     health.maxHealth += 1;
     yield WaitForSeconds(.08);
     health.maxHealth += 1;
     yield WaitForSeconds(.08);
     health.maxHealth += 1;
     yield WaitForSeconds(.08);
     health.maxHealth += 1;
     yield WaitForSeconds(.0);
     health.maxHealth += 1;
     
     
avatar image
0

Answer by EliteMossy · Apr 05, 2013 at 11:58 PM

 for (var i = 0; i < sodaHealth; i++){
 health.maxHealth++;
 yield WaitForSeconds(0.8f);
 }

Is a much better way to do it than what you did. Look at how much less code. I mean i would probably spend time and think of a better way. But my code is a lot tidier.

Comment
Add comment · Show 3 · 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 inglipX · Apr 06, 2013 at 12:04 AM 0
Share

your code adds 1 and doesn't stop adding it

avatar image EliteMossy · Apr 06, 2013 at 12:26 AM 0
Share

what you mean it does not stop adding it? There is no logical reason for it carry on past sodaHealth (which is 9) so it will do it 9 times (0-8) then exit the for loop.

avatar image Chronos-L · Apr 06, 2013 at 12:38 AM 0
Share

If this code is put in the Update(), the yield wouldn't work. It has to be put in a function.

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

14 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

Related Questions

Enemy Health Help 1 Answer

Timer help please 1 Answer

Death upon health = 0 2 Answers

Destroying Enemy Help 5 Answers

A node in a childnode? 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