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 FPSworrior · Dec 08, 2013 at 11:46 PM · triggerhealth-deductionhealth

My health decreases to fast.

I want it where if I stand in the trigger than my health will go down slowly, but I don't know how to make it go slowly. I used a int value to make it a little slower which didn't really slow it down much, so I tried a float value, but it says that I cannot use a float value. Somebody help. Heres my code. It's line 52 where I'm having the problem.

 using UnityEngine;
 using System.Collections;
 
 public class Soldier : MonoBehaviour {
 
     public int health = 100;
     public int hitSpeed = 1;
     public GUIText healthText;
     public GUIText dead;
     public bool canPickUpHealth;
     public bool isHit = false;
     public bool isDead = false;
 
 
 
     // Use this for initialization
     void Start () {
         SetHealthText();
         dead.text = "";
         canPickUpHealth = false;
         isDead = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(health >= 100)
         {
             health = 100;
             SetHealthText();
         }
         if(health < 0)
         {
             health = 0;
             SetHealthText();
         }
         if(health <= 0)
         {
             Time.timeScale = 0;
             dead.text = "YOU ARE DEAD";
             SetHealthText();
         }
         if(health == 100)
         {
             canPickUpHealth = false;
         }
         else
         {
             canPickUpHealth = true;
         }
         if(isHit)
         {
             health -= 15 * hitSpeed;
             SetHealthText();
         }
         else
         {
             isHit = false;
         }
 
 
 
     
     }
 
     void OnTriggerEnter(Collider other)
     {
         if(canPickUpHealth)
         {
             if(other.gameObject.tag == "Health" && health <= 100)
            {
                 other.gameObject.SetActive(false);
                 health = health + 15;
                 SetHealthText();
            }
         }
         if(other.gameObject.tag == "Enemy" && health > 0)
         {
             isHit = true;
         }
 
     }
 
     void OnTriggerStay(Collider other)
     {
         if(other.gameObject.tag == "Enemy")
         {
             isHit = false;
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if(other.gameObject.tag == "Enemy")
         {
             isHit = false;
         }
     }
 
     void SetHealthText()
     {
         healthText.text = "Health: " + health.ToString();
         if(health <= 0)
         {
             dead.text = "YOU ARE DEAD";
         }
     }
     
 }
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
Best Answer

Answer by instruct9r · Dec 08, 2013 at 11:55 PM

first, change the declaration of health and hitSpeed to float. It tells you, that you can't use float, because you can't multiply int with float.

 public float health = 100.0f;
 public float hitSpeed = 1.0f;

then change the line where the health shoud go down

  health -= Time.deltaTime * hitSpeed;

And then you adjust the speed with the hitSpeed attribute...

Comment
Add comment · Show 9 · 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 FPSworrior · Dec 08, 2013 at 11:56 PM 0
Share

doesn't work. /Scripts/Soldier.cs(52,25): error CS0266: Cannot implicitly convert type float' to int'. An explicit conversion exists (are you missing a cast?)

avatar image instruct9r · Dec 08, 2013 at 11:58 PM 0
Share

Sorry, i've just edited answer :)

avatar image FPSworrior · Dec 09, 2013 at 12:33 AM 0
Share

This is good but is there any possible way to use a int value because when I get hurt, my health goes to something like 97.4567 ins$$anonymous$$d of just 97.

avatar image instruct9r · Dec 09, 2013 at 12:39 AM 0
Share

ok. You can convert the result number to a whole number with the $$anonymous$$athf functions. For example, the $$anonymous$$athf.Round. This will round the number to the nearest whole number. 10.1 = 10, 10.6 = 11, etc, etc.

So it shoud be something like this:

 if(isHit)
        {
          health -= Time.deltaTime * hitSpeed;
          health = $$anonymous$$athf.Round(health);
          SetHealthText();
        }


Or you can create one new int variable and assing it with the rounded number. Note, that you'll have to change the health with healthInt in the SetHealthText function, to display the proper number...

 public int healthInt;
 
 if(isHit)
            {
              health -= Time.deltaTime * hitSpeed;
              healthInt = $$anonymous$$athf.RoundToInt(health);
              SetHealthText();
            }


If you don't want to round the number, you can use some of the other $$anonymous$$athf functions. http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=$$anonymous$$athf

Check the Ceil, CeilToInt, Floor, FloorToInt.

avatar image instruct9r · Dec 09, 2013 at 12:58 AM 0
Share

One other way might work, if you include F0 in the brackets right after the ToString(). F0 will display only the whole number (if i remember right). F1 will display one number after the decimal and so on. So try just replacing the piece of code below, without using the $$anonymous$$athf function.

 void SetHealthText()
 {
    healthText.text = "Health: " + health.ToString("F0");
    if(health <= 0)
    {
      dead.text = "YOU ARE DEAD";
    }
 }

Anyway, this way i'm not sure how the number will be rounded. To the nearest or to the ceil / floor

Show more comments

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

Multiple Cars not working 1 Answer

OnTriggerStay 2 Answers

How to trigger a Script from an Object. 0 Answers

How to make the trigger work only once. (SOUND) 1 Answer

Health does not update in this script, why? 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