Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hoang Tuan · Mar 12, 2014 at 08:31 AM · timeshootingpower

Time in unity

It's my code to creat a simple power bar , i want when i press jump the power increasing and when i release then the power bar return 0 , but my problem it's in the line 30 , the Time never reset to 0 and continue count again from start , what Time can i use in this case , tks :D ?

 using UnityEngine;
 using System.Collections;
 using AssemblyCSharp;
 
 public class PowerBar : MonoBehaviour {
     public float fullWidth = 100; //max width of the powerbar
     private float thePower; //current power
 
     public bool increasing = false;
     private bool jumping = false;
 
     public float barSpeed = 1; //how fast bar will fill in.
 
     // Use this for initialization
     void Start () {
         thePower = 0;
     }
     // Update is called once per frame
     void Update () {
         if (!jumping) {
                 if (Input.GetButton ("Jump")) { //if jump button is held down se increasing to true.
                         increasing = true;
                 } else if (Input.GetButtonUp ("Jump")) {
                         increasing = false; //set bar increasing back to false.
                         GV.trueShot = true;
                 }
         }
         if(increasing) //if bar is increasing, calculate thepower
         { 
             thePower = Time.time * barSpeed;
         }
         else //else set thepower back to 0.
         {
             thePower=0;
         }
 
         if (thePower > 0) {
             AddjustCurrentPower (thePower);
         }
     }
     void OnGUI(){
         if (Input.GetButton ("Jump")) {
             GUI.Box (new Rect (10, 10, thePower, 20), "");
         } else {
             GUI.Box (new Rect (10, 10, 1, 20), "");
         }
     }
 
     public void AddjustCurrentPower(float adj){
         GV.PowerBar += adj;
         if(GV.PowerBar <0)
             GV.PowerBar = 0;
         if(GV.PowerBar > fullWidth)
             GV.PowerBar = fullWidth;
         if(fullWidth <1)
             fullWidth = 1;
         thePower =(Screen.width /2) * (GV.PowerBar / (float)fullWidth);
     }
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by haim96 · Mar 12, 2014 at 08:39 AM

i think your problem is here:

 } else if (Input.GetButtonUp ("Jump")) {
                  increasing = false; //set bar increasing back to false.
                  debug.log("released"); // add this for test ****
                  GV.trueShot = true;


add a line of debug.log("released"); to make sure that that increasing = false; is happening.

Comment
Add comment · Show 8 · 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 haim96 · Mar 12, 2014 at 08:44 AM 0
Share

beside that there no reason not to short every thing to this:

  if (!jumping) {
               if (Input.GetButton ("Jump")) { 
                      thePower = Time.time * barSpeed;
               } else  {
                      thePower = 0;
                      GV.trueShot = true;
               }
            }
avatar image Hoang Tuan · Mar 12, 2014 at 09:08 AM 0
Share

Tks for your help :D , but i try debug.log in the line and the problem it's the value Time.time is still too large and my power bar is fill too quick @@

avatar image haim96 · Mar 12, 2014 at 09:18 AM 0
Share

try to swap timt.time in time.deltatime...

http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html

http://docs.unity3d.com/Documentation/ScriptReference/Time-deltaTime.html

avatar image Hoang Tuan · Mar 12, 2014 at 09:40 AM 0
Share

I was swap time.time in time.deltatime but it get the same result @@ is that anyway to reset time.time or time.deltatime :D

avatar image haim96 · Mar 12, 2014 at 09:47 AM 0
Share

no way to do that. read the links i post to see why... but you could also set the barspeed to 0 then timt.time * 0 will be always 0...

Show more comments
avatar image
0

Answer by DuckOfDoom · Mar 12, 2014 at 09:45 AM

You should create a private float variable, then each Update() call add Time.deltaTime(which is time from last frame) to it and use it like you use your Time.time. When you need to reset the timer, just assign zero to it.

Comment
Add comment · Show 3 · 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 Hoang Tuan · Mar 12, 2014 at 10:02 AM 0
Share

I do like you said but the time.time still runing , in here i use the float barTime , i reset it to 0 when release the button but because the time.deltatime still running then the bar it's not running from begin like i want T_T

 using UnityEngine;
 using System.Collections;
 using AssemblyCSharp;
 
 public class PowerBar : $$anonymous$$onoBehaviour {
     public float fullWidth = 100; //max width of the powerbar
     private float thePower; //current power
     
     public bool increasing = false;
     private bool jumping = false;
     
     public float barSpeed = 1; //how fast bar will fill in.
 
 
     private float _barTime;
     // Use this for initialization
     void Start () {
         _barTime = 0f;
         thePower = 0;
     }
     // Update is called once per frame
     void Update () {
 
         _barTime += Time.deltaTime/10;
 
         if (!jumping) {
             if (Input.GetButton ("Jump")) { //if jump button is held down se increasing to true.
                 thePower = _barTime;
                 Debug.Log(_barTime);
             } else if (Input.GetButtonUp ("Jump")) {
                 thePower=0;
                 _barTime=0;
 
                 GV.trueShot = true;
             }
         }
         if (thePower > 0) {
             AddjustCurrentPower (thePower);
         }
     }
     void OnGUI(){
         GUI.Box (new Rect (10, 10, thePower, 20), "");
         Debug.Log (thePower);
     }
     
     public void AddjustCurrentPower(float adj){
         GV.PowerBar += adj;
         if(GV.PowerBar <0)
             GV.PowerBar = 0;
         if(GV.PowerBar > fullWidth)
             GV.PowerBar = fullWidth;
         if(fullWidth <1)
             fullWidth = 1;
         thePower =(Screen.width /2) * (GV.PowerBar / (float)fullWidth);
     }
 }
avatar image DuckOfDoom · Mar 12, 2014 at 10:19 AM 0
Share

You should increase _barTime only when you need your bar filling up.

Try this, line 30:

 else if (Input.GetButtonUp ("Jump")) 
 {
           thePower = 0;
           _barTime = -1;
 }

Then, line 24:

 if (_barTime >= 0)  
     _barTime += Time.deltaTime/10;

Finally, line 27:

              if (Input.GetButton ("Jump")) {
               _barTime = 0;
               thePower = _barTime;
 

The point is - we use negative value of _barTime to deter$$anonymous$$e if the bar is active and are adding something to it only when it isn't. Thus, when we set it to 0, the bar will fill, if we want to stop it, we set it to any negative number.

avatar image Hoang Tuan · Mar 13, 2014 at 02:32 AM 0
Share

tks for your help so much when show me my problem :D

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Digital Clock: Time.time > 60 2 Answers

Bullets sometimes shoot under map 1 Answer

How do you make a shoot up feature like in dx-ball? 2 Answers

I am trying to get my enemy to follow and shoot at my fps i have a script but i am geting errors can someone help me please 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