Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by NihkValentine · Mar 12, 2017 at 03:18 PM · floatintdisplay

float number displayed as an int? or at least on the health bar

So I got a health bar, hunger bar, and thirst bar. everything is good; the thirst and hunger goes down throughout time. Also got it hooked up to the canvas, so I see my visual hunger and thirst bars gradually go down, along with the %.

problem is, it displays all these crazy ass decimal numbers after the decimal. I want to cut it down to 2 digits after the decimal, or anything like that.

and in order for the visual bars to be hooked up to the invisible variable numbers, it has to be a float?

heres the script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class DamageAI : MonoBehaviour { public float playerHealth = 100; private float maxplayerHealth = 100; private float damage = 8;

 //New shit xD
 private EnemyAI DAI;
 private EnemyAI EAI;
 public AudioSource aSource;
 private int healthReg = 1;
 bool isRegenHealth;

 public float hunger = 100f;
 private float maxHunger = 100;
 public float thirst = 100f;
 private float maxThirst = 100;

 private float hungerSpeedMultiplier = 0.055f;
 private float thirstSpeedMultiplier = 0.155f;


 void Start()
 {
     UpdateHealthBar();
     aSource = GetComponent<AudioSource>();
     EAI = gameObject.GetComponent<EnemyAI>();
     DAI = gameObject.GetComponent<EnemyAI>();
 }

 private void Update()
 {
         {
             UpdateHealthBar();
             CheckDeath();
             UpdateHNT();
         }
     if (hunger > 0)
     {
         hunger -= Time.deltaTime * hungerSpeedMultiplier;
     }
     if (thirst > 0)
     {
         thirst -= Time.deltaTime * thirstSpeedMultiplier;
     }
 }

 private void CheckDeath()
 {
     if (playerHealth <= 0)
     {
         Die();
     }
 }

 private void Die()
 {
     Destroy(GetComponent<Movement>());
     Destroy(GetComponent<EnemyAI>());
     Destroy(GetComponent<DamageAI>());
 }

 private void UpdateHealthBar()
 {
     if(playerHealth != maxplayerHealth && !isRegenHealth)
     {
         StartCoroutine(RegainHealthOverTime());
     }
     if (playerHealth > maxplayerHealth)
     {
         playerHealth = maxplayerHealth;
     }
 }

 private IEnumerator RegainHealthOverTime()
 {
     isRegenHealth = true;
     while (playerHealth < maxplayerHealth)
     {
         Healthregen();
         yield return new WaitForSeconds(3);
     }
     isRegenHealth = false;
 }

 public void Healthregen()
 {
     playerHealth += healthReg;
 }

 private void UpdateHNT()
 {
     if(hunger > maxHunger)
     {
         hunger = maxHunger;
     }
     if(thirst > maxThirst)
     {
         thirst = maxThirst;
     }

         if (hunger <= 0 && (thirst <= 0))
         {
             playerHealth -= Time.deltaTime / damage * 16;
         }

         else if (hunger <= 0 || thirst <= 0)
         {
             playerHealth -= Time.deltaTime / damage * 16;
         }

         if(hunger < 50f)
     {
         EAI.lookAtF();
         EAI.eatPlease();
     }

         if(thirst < 50f)
     {
         DAI.lookAtD();
         DAI.drinkPlease();
     }
     }

 void OnCollisionEnter(Collision _collision)
 {
     if (_collision.gameObject.tag == "Enemy")
     {
         playerHealth -= damage;
         aSource.Play();
         {
             if (playerHealth < 0)
             {
                 playerHealth = 0;
             }
             UpdateHealthBar();
         }
     }
 }

 public void HealDamage(float heal)
 {
     playerHealth += heal;
     if (playerHealth > maxplayerHealth)
         playerHealth = maxplayerHealth;

     UpdateHealthBar();
 }

}

Comment
Add comment · Show 1
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 NihkValentine · Mar 12, 2017 at 02:15 AM 0
Share

Ok maybe that was too much. That was the whole script. Here are the important parts:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class DamageAI : $$anonymous$$onoBehaviour { public float playerHealth = 100; private float maxplayerHealth = 100; private float damage = 8;

 public float hunger = 100f;
 public float thirst = 100f;

 private float hungerSpeed$$anonymous$$ultiplier = 0.055f;
 private float thirstSpeed$$anonymous$$ultiplier = 0.155f;


 void Start()
 {
 }

 private void Update()
 {
         {
             UpdateHNT();
         }
     if (hunger > 0)
     {
         hunger -= Time.deltaTime * hungerSpeed$$anonymous$$ultiplier;
     }
     if (thirst > 0)
     {
         thirst -= Time.deltaTime * thirstSpeed$$anonymous$$ultiplier;
     }
 }

 private void UpdateHNT()
 {

         if (hunger <= 0 && (thirst <= 0))
         {
             playerHealth -= Time.deltaTime / damage * 16;
         }

         else if (hunger <= 0 || thirst <= 0)
         {
             playerHealth -= Time.deltaTime / damage * 16;
         }
     }
 }

}

0 Replies

· Add your reply
  • Sort: 

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

94 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Adding Int's from different and the same script 1 Answer

How to make a number higher than Quintillion? (1000000000000000000) 1 Answer

ToString ("f0") is rounding my float 1 Answer

Error CS0266 with my clicker game 1 Answer

Really need help please/A switch expression of type `float' cannot be converted to an integral type, bool, char, string, enum or nullable type 2 Answers


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