• 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 : MonoBehaviour { public float playerHealth = 100; private float maxplayerHealth = 100; private float damage = 8;

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

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


 void Start()
 {
 }

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

 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Convert "Double" to "Float" or "Int"? 1 Answer

Convert Float to Int for Google leaderboard 1 Answer

Float surprisingly turns to int or 0 1 Answer

Adding a int and a float? 2 Answers

Optimization Question - Floats or Ints? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges