- Home /
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);
}
}
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.
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;
}
}
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 @@
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
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
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...
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.
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);
}
}
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.
Your answer
Follow this Question
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