• 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 joeross0 · Dec 31, 2017 at 12:55 AM · floatinttime.deltatimefloatingpointhealth

Why is this Int lagging when its subtracting something over a period of time?

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 
 public class hthScript : MonoBehaviour {
 
     //begining
     public int maxHealth = 100;
     public int maxThirst = 100;
     public int maxHunger = 100;
 
     //realtime
     public int currentHealth;
     public int currentThirst;
     public int currentHunger;
     public float hungerLoss;
     public float thirstLoss;
 
     //references
     public Slider healthSlider;
     public Slider thirstSlider;
     public Slider hungerSlider;
 
 
 
 
 
     // Use this for initialization
     void Start () {
         currentHealth = maxHealth;
         currentThirst = maxThirst;
         currentHunger = maxHealth;
     }
     
     // Update is called once per frame
     void Update () {
         HTHDecline();
 
         healthSlider.value = currentHealth;
         thirstSlider.value = currentThirst;
         hungerSlider.value = currentHunger;
     }
 
 
 
 
 
 
     //when something happens:
     public void TakeDamage(int amount)
     {
         currentHealth -= amount;
     }
 
     public void DrinkWater(int amount)
     {
         currentThirst += amount;
     }
 
     public void EatFood(int amount)
     {
         currentHunger += amount;
     }
 
 
 
 
 
     //Over a period of time:
     public void HTHDecline()
     {
 
         //For hunger
         currentHunger = currentHunger - Mathf.RoundToInt(Time.deltaTime * hungerLoss);
 
         //For thirst
         currentThirst = currentThirst - Mathf.RoundToInt(Time.deltaTime * thirstLoss);
 
         //thirst & hunger kills you:
         if (currentHunger < 25)
         {
             currentHealth = currentHealth - Mathf.RoundToInt(Time.deltaTime * 20);
         }
         if (currentThirst < 25)
         {
             currentHealth = currentHealth - Mathf.RoundToInt(Time.deltaTime * 20);
         }
     }
 }
 
 
Comment
Add comment · Show 3
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 joeross0 · Dec 31, 2017 at 12:56 AM 0
Share

It will count down from 100 and stop at random numbers and continue on the currentHunger and the currentThirst variables.

avatar image jdean300 · Dec 31, 2017 at 02:23 AM 0
Share

I don't completely understand what you're asking, but currentHealth = currentHealth - $$anonymous$$athf.RoundToInt(Time.deltaTime * 20); seems like a problem. Unless your game is running slow, $$anonymous$$athf.RoundToInt(Time.deltaTime * 20); will always equal 0.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by joeross0 · Dec 31, 2017 at 03:56 AM

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

public class hthScript : MonoBehaviour {

 //begining
 public float maxHealth = 100;
 public float maxThirst = 100;
 public float maxHunger = 100;

 //realtime
 public float currentHealth;
 public float currentThirst;
 public float currentHunger;
 public float hungerLoss;
 public float thirstLoss;

 //references
 public Slider healthSlider;
 public Slider thirstSlider;
 public Slider hungerSlider;





 // Use this for initialization
 void Start()
 {
     currentHealth = maxHealth;
     currentThirst = maxThirst;
     currentHunger = maxHealth;
 }

 // Update is called once per frame
 void Update()
 {
     HTHDecline();
     

     healthSlider.value = currentHealth;
     thirstSlider.value = currentThirst;
     hungerSlider.value = currentHunger;
 }






 //when something happens:
 public void TakeDamage(int amount)
 {
     currentHealth -= amount;
 }

 public void DrinkWater(int amount)
 {
     currentThirst += amount;
 }

 public void EatFood(int amount)
 {
     currentHunger += amount;
 }





 //Over a period of time:
 public void HTHDecline()
 {
     //For hunger
     currentHunger = currentHunger - Time.deltaTime * hungerLoss;

     //For thirst
     currentThirst = currentThirst - Time.deltaTime * thirstLoss;

     //thirst & hunger kills you:
     if (currentHunger < 25)
     {
         currentHealth = currentHealth - Time.deltaTime * 20;
     }
     if (currentThirst < 25)
     {
         currentHealth = currentHealth - Time.deltaTime * 20;
     }

 }

}

Sooooo i found out what was wrong. It basically wasn't running smooth so I had to change all of the int's to float's

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

73 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

Related Questions

Rounding to the nearest int as a Result of a Function? 1 Answer

Turn float to int 2 Answers

Float input output problem? 1 Answer

Have a problems with a values 1 Answer

How to submit float score as time to Game Center? 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