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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Brendan7125 · Jul 19, 2012 at 03:05 AM · regenerationhealth bar

Deducting a value from a total vaule

Hi all, just a quick question in relation to scripting for a game I'm currently designing: I want the main player character to have a total 'health' bar and every second a weapon is fired (the mouse is held down) a value is deducted from the total vaule.

For example: - total health of 100 - the mouse is held down for 5 seconds and the main machine gun is fired - 2 health is taken away for each second

Also once 1 second has elapsed the health has to recharge at a rate of 5 health per second.

How would I go about scripting this...? Just a basic code that I can build on myself (I'm quite new to scripting!) Thanks!

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 aldonaletto · Jul 19, 2012 at 03:27 AM

Time.deltaTime is what you need for both things. I don't know much about your code, but supposing the variable beingShot is true when the player is being hit, you could do something like this:

var healRate: float = 5; var damageRate: float = 2; private healTime: float = 0;

function Update(){ if (beingShot){ // while player under fire... health -= damageRate Time.deltaTime; // decrement health at damageRate healTime = Time.time+5; // and set healing start to 5 seconds from now } else // but when not being shot... if (Time.time > healTime){ // and if it's time to start healing... health += healRate Time.deltaTime; // increment health at healRate } health = Mathf.Clamp(health, 0, 100); // clamp health to the 0..100 range }

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 Brendan7125 · Jul 19, 2012 at 10:50 PM 0
Share

I think I need to rephrase my question:

The way I've mixed up that strategy of my game is that you, a space probe, have a total 100 energy. This energy is used as 'resource' to fire weapons and move around the screen. Every second the space probe's machine gun is fired (left mouse click), 2 energy points are deducted from the total energy value of 100 every second the mouse is held down. Every second the movement button(s) are held down (left, right, up, down) 5 health is taken away from the energy value. Then once there is no activity (that is, no weapons fired, no movement) for 1 second, the health is to regenerate by 5 points per second.

Hope that clears things up a bit!

If you could put notes in the script to help me change the script for different types of weapons (i.e. change the instantiate machinegunfire to a missile...?)

Thanks!!!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Recharge delay on shield regen through coroutines 2 Answers

GUI box scaling issue [C#] 0 Answers

My health bars HealthBar.fillAmount doesnt change.tnx,i nead a help in fillamount :pls my HealthBar doesnt change. i dont know what to do.pls:) 2 Answers

Health bar help 1 Answer

(Health bar) Remove lives properly 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