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 hyperscope · Jul 20, 2014 at 02:20 AM · javascriptdamagehealth

Damage/Healing over time

After referencing numerous relevant scripts in a attempt to Frankestein my own to life I have hit a wall. My intention is to have the player affected by the health modifiers over time instead of an instance as it currently set. I believe it requires OnTriggerEnter and OnTriggerExit and possibly something to do with Time.deltatime?

Just starting out with Unity programming so please forgive my ignorance any help or insight would be greatly appreciated.

 var playerHealth = 100;
     function Update () {
         if(playerHealth <= 0){
             playerHealth = 0;
             killPlayer();
             }

 function OnTriggerEnter (other : Collider){
     if (other.gameObject.CompareTag("Black Orbit"))
         {
         playerHealth -= 2;
         }
     if (other.gameObject.CompareTag("White Orb"))
         {
         playerHealth += 1;
         }}



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
0

Answer by Wakeful · Jul 20, 2014 at 12:31 PM

I normally use c# but try :

 {
 playerhealth = playerhealth - Time.deltaTime;
 }

And switch OnTriggerEnter to OntriggerStay Thats if u only want it to subtract health when on contact with that object. If you want it to constantly lose health:

 if(other.gameObject.CompareTag("Black Orb"))
 {
 damaging = true;
 }
 
 if(damaging == true)
 {
 playerhealth = playerhealth - Time.deltaTime;
 }


Try that! Sorry if this doe not work, I'm on a mobile right now and can't check.

Comment
Add comment · Show 5 · 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 hyperscope · Jul 20, 2014 at 04:44 PM 0
Share

Simply changing trigger to OnTriggerStay solved the basic mechanics of my intended function. Now I would like to modify the rate at which it increases or decreases. I tried using your code snippet:

playerhealth = playerhealth - time.deltatime;

In both the update and trigger functions, it reported "$$anonymous$$ identifier: 'time'." So I tried adding:

var time = #

where # is a random number I imputed but that doesn't seem to effect the rate.

I really appreciate yourself taking the time to $$anonymous$$ch me some things and will be happy to pay it forward as soon as I can!

avatar image AlucardJay · Jul 20, 2014 at 04:58 PM 0
Share

Just a typo : Time.deltaTime

http://docs.unity3d.com/ScriptReference/Time-deltaTime.html

avatar image CScherer549 · Jul 20, 2014 at 05:18 PM 0
Share

it is Time with a capital T. If you use that number value and try playerhealth -= Time.deltatime * yourNumber;

that should have the desired effect!

avatar image Wakeful · Jul 20, 2014 at 09:59 PM 0
Share

Ya sorry about the typo! I was on a mobile...

avatar image Kiwasi · Jul 21, 2014 at 12:30 AM 0
Share

Typos fixed. To change the rate of damage simply multiply Time.deltaTime by your damage per second.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Health Question 1 Answer

Damage script 0 Answers

The enemy don´t lose health, but why??? 0 Answers

Looking for skilled individual to assist with problem 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