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 /
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.

avatar image joeross0 jdean300 · Dec 31, 2017 at 03:48 AM 0
Share

Basically im trying to make the hunger slider slowly count down and when the hunger bar is at 25 it will start to hurt the player. For some reason the hunger bar will start to count down and stop and go again.

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.

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

"Talent Tree" global yes/false var and int? 3 Answers

Cannot convert 'callable(int) as float' to 'float'. 1 Answer

Whole Numbers Divide Incorrectly....BUG? 0 Answers

Multiply float by int? 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