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 sporkflips · Mar 03, 2016 at 03:23 PM · c#destroyvariablefloat

C# float variable isnt behaving like its value

I have this set up so that my character has an energy bar that drains over time. I'm trying to set it so that if it's below 50% it drains faster, then same with below 30% and 20% with < 20% draining very quickly. about .008% per frame, I think. (the normal drain rate is about .0004% per frame) if I watch the energyDrainSpeed in the inspector while running my game, I can see it increasing but it doesn't behave the same as the number by itself does. what I mean is... if I change the DrainEnergy function so that there's just 500.0f instead of energyDrainSpeed, the character's energy drains a lot faster than it does when energyDrainSpeed is equal to 500.0f (or higher) I also tried setting the energyDrainSpeed to over 12000.0f but it definitely wasn't draining at a speed of 1% per frame. then when I replaced the variable with just 12000 in the script - like curEnergy - (12000 / 12000) - it did drain at a rate of 1% per frame.

Am I just getting lag or something?

this is the code I'm working on;

 using UnityEngine;
 using System.Collections;
 
 public class PlayerEnergy : MonoBehaviour  
 {  
     public float maxEnergy = 100;  
     public float curEnergy = 100;  
     public float energyDrainSpeed = 5.0f;
 
     public float energyBarLength;
 
     void Start()
     {
         energyBarLength = Screen.width / 5;
     }
 
     void Update()
         {
             DrainEnergy();
             AdjustCurrentEnergy(0);
     
             if (curEnergy < 50)
             {
                 energyDrainSpeed = energyDrainSpeed + 100.0f;
             }
             if (curEnergy < 30)
             {
                 energyDrainSpeed = energyDrainSpeed + 300.0f;
             }
             if (curEnergy < 20)
             {
                 energyDrainSpeed = energyDrainSpeed + 500.0f;
             }
         }

         void OnGUI()
         {
                  GUI.Box(new Rect(10, 35, energyBarLength, 20), curEnergy + "/" + maxEnergy);
         }  
     
            private void DrainEnergy()
         {
             curEnergy = Mathf.Clamp(curEnergy - (energyDrainSpeed / 12000) , 0.0f, maxEnergy);
         }  
         public void AdjustCurrentEnergy(int adj)
         {
             curEnergy += adj;
     
             if (curEnergy < 0)
                 curEnergy = 0;
     
             if (curEnergy > maxEnergy)
                 curEnergy = maxEnergy;
     
             if (maxEnergy < 1)
                 maxEnergy = 1;
     
             energyBarLength = (Screen.width / 5) * (curEnergy / (float)maxEnergy);
         }
     }
 }

Comment
Add comment · Show 2
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 Landern · Mar 03, 2016 at 03:35 PM 0
Share

Did you intend on your energy drain speed to stack like that from 50 to 20? If curEnergy is less than 20 then all three if statements affect energyDrainSpeed in one Update call(per frame).

Did you mean something like:

  if (curEnergy <= 50 && curEnergy > 30)
  {
      energyDrainSpeed = energyDrainSpeed + 100.0f;
  }
  else if (curEnergy <= 30 && curEnergy > 20)
  {
      energyDrainSpeed = energyDrainSpeed + 300.0f;
  }
  else if (curEnergy <= 20)
  {
      energyDrainSpeed = energyDrainSpeed + 500.0f;
  }
avatar image sporkflips Landern · Mar 04, 2016 at 05:08 AM 0
Share

Yeah I did that on purpose, although I can't remember exactly why... I do remember having some reason for it though aha. I'll probably readdress that since I intend to add in a lot of other variables that influence the drain speed. but for now the issue is that it's not draining fast enough even in spite of the stacking.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by high500 · Mar 03, 2016 at 07:20 PM

Am I wrong or ae you adding more frames as it goes lower? surely it should be less ie. + 100.0f, + 33.0f + 20.0f

I'm probably way off just an observation.

Comment
Add comment · Show 1 · 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 sporkflips · Mar 04, 2016 at 05:16 AM 0
Share

I'm not sure what you mean about adding more frames. are you referencing what's inside the $$anonymous$$athf.Clamp? if so, I'm subtracting from curEnergy based on the energyDrainSpeed in relation to a constant.

the drain does work and it goes down at a good rate for how I want it to drain starting from 100 energy, it's just that the equation inside $$anonymous$$athf.Clamp doesn't seem to be working when I use the energyDrainSpeed variable; the drain barely increases in speed when I run the game no matter what the curEnergy level is, unless I replace energyDrainSpeed with a constant that's equal to what energyDrainSpeed is equal to when curEnergy is below 50 or 20

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why is it that when I added a C sharp script to a game object, the game object disappeared? 0 Answers

C# decreasing float slightly incorrect (0.4999999) 1 Answer

Float surprisingly turns to int or 0 1 Answer

float value is reset after every update 1 Answer

A strange question go.IsActive() 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