Time.time is not working properly

HI.
I have a script where the player is able to fire a shot after pressing the mouse button. However the counter for the time is not working. It is supposed to reset after each shot is fired however it is continually updating. Even when the mouse button is not down. So my q is how do I get the time to count only when the mouse is pressed and reset the counter after MouseButtonUp is used.

//inspector variables
var chargedProjectile 	: Transform;
var basicProjectile 	: Transform;
var muzzle 				: Transform;

var chargingTimer 		: float = 0;
var charged				: float = 3;

function Update()
{
	//chargingTimer is seconds dependant not frame.
	//var chargingTimer : float = Time.deltaTime * 2;
	 //when the mouse button clicks charging begins
	 if(Input.GetButtonDown("Fire1"))
	 {
	 	if(chargingTimer == 0)
	 	{
	 		chargingTimer += Time.time;
	 		Debug.Log ("Charging timer works");
	 		print("The chargingTime value is" + chargingTimer);
	 	}
	 }
}	 
function LateUpdate()	 
{
	 //what happens when it is charged
	 if(Input.GetButtonUp("Fire1"))
	 {
	 	if(chargingTimer < charged)
	 	{
	 		//play animation
	 		Instantiate(basicProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the basicProjectile");
	 		chargingTimer = 0;
	 	}
	 	else if(chargingTimer > charged)
	 	{
	 		//play animation
	 		Instantiate(chargedProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the chargedProjectile");
	 		chargingTimer = 0;
	 	}
	 }
}

Try replacing it with the deltatime function. Time is time from application/level start, deltatime is time since last frame.

I have tried doing that but it does not count in seconds. I want it to after 3 seconds change from a basic shot to a charged shot. deltaTime does frames and it does not increase the number past 0.04

For example have the counter start at zero and when the mouse button i spressed the counter starts counting in increments of one. If a shot if fired before it hit 3 its a basic shot but if it is fired after the counter passes 3 it switches to a different shot. After the shot is fired the counter then resets to zero. Does that explain it better?

Try replacing this line in your Input.GetButtonDown

if(chargingTimer == 0)

with this

if(chargingTimer < charged)

Also, if you want to do hold to charge, you’ll want to use Input.GetButton. Input.GetButtonDown is only called once during the frame when the user has pressed the button.

Thanks so much for the help. My problem is that the time isn’t resetting to zero on the release of the mouse button.

Updated code. As mentioned above the timer is nnot resetting to zero when the mouse button is released.

//inspector variables
var chargedProjectile 	: Transform;
var basicProjectile 	: Transform;
var muzzle 				: Transform;

var chargingTimer 		: float = 0;
var charged				: float = 3;

function Update()
{
	//chargingTimer is seconds dependant not frame.
	//var chargingTimer : float = Time.deltaTime * 2;
	 //when the mouse button clicks charging begins
	 if(Input.GetButton("Fire1"))
	 {
	 	if(chargingTimer < charged)
	 	{
	 		chargingTimer += Time.time;
	 		Debug.Log ("Charging timer works");
	 		print("The chargingTime value is" + chargingTimer);
	 	}
	 }
}	 
function LateUpdate()	 
{
	 //what happens when it is charged
	 if(Input.GetButtonUp("Fire1"))
	 {
	 	if(chargingTimer < charged)
	 	{
	 		//play animation
	 		Instantiate(basicProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the basicProjectile");
	 		chargingTimer = 0;
	 	}
	 	else if(chargingTimer > charged)
	 	{
	 		//play animation
	 		Instantiate(chargedProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the chargedProjectile");
	 		chargingTimer = 0;
	 	}
	 }
}

Must you put it in LateUpdate? What happens if you put it in Update? Also, doing < charged and > charged in your else statement will neglect = charged if it ever hits that condition in the most luckiest of situation. Just do a regular if-else instead of if-else-if

I changed it like you requested but the chargingTimer is still not resetting to zero. It starts counting as soon as the level loads but it should only be doing it when the mouse button is pushed down.

//inspector variables
var chargedProjectile 	: Transform;
var basicProjectile 	: Transform;
var muzzle 				: Transform;

var chargingTimer 		: float = 0;
var charged				: float = 3;

function Update()
{
	//chargingTimer is seconds dependant not frame.
	//var chargingTimer : float = Time.deltaTime * 2;
	 //when the mouse button clicks charging begins
	 if(Input.GetButton("Fire1"))
	 {
	 	if(chargingTimer < charged)
	 	{
	 		chargingTimer += Time.time;
	 		Debug.Log ("Charging timer works");
	 		print("The chargingTime value is" + chargingTimer);
	 	}
	 }
	  //what happens when it is charged
	 if(Input.GetButtonUp("Fire1"))
	 {
	 	if(chargingTimer < 3)
	 	{
	 		//play animation
	 		Instantiate(basicProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the basicProjectile");
	 		chargingTimer = 0;
	 	}
	 	else if(chargingTimer > 3)
	 	{
	 		//play animation
	 		Instantiate(chargedProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the chargedProjectile");
	 		chargingTimer = 0;
	 	}
	 }
}

“chargingTimer += Time.time;” is wrong, it will give you the total time since the start of the game.

“Description
The time this frame has started (Read Only). This is the time in seconds since the start of the game.”

As stated before you should use deltaTime instead or the timer will always fill up every frame.
Also the last else if might just be a else.

Ok but it is supposed to start when the mousebutton is pressed. Not to run at the beginning of the game. How do I get it to do that? Second I have used deltaTime but it does not increase at all. It stays in roughly the same ball park figure of 0.06. I cannot get it to count time to a certain number. So is there a way i can keep track of time that will allow me to use an attack or not?

So if you run this code, what do you get in your Console output when pressing and not pressing the fire button??

    //inspector variables
    var chargedProjectile   : Transform;
    var basicProjectile     : Transform;
    var muzzle              : Transform;
     
    var chargingTimer       : float = 0;
    var charged             : float = 3;
     
    function Update()
    {
        //chargingTimer is seconds dependant not frame.
        //var chargingTimer : float = Time.deltaTime * 2;
         //when the mouse button clicks charging begins
         if(Input.GetButton("Fire1"))
         {
            if(chargingTimer < charged)
            {
                chargingTimer += Time.deltaTime;
                Debug.Log (chargingTimer);
                Debug.Log ("Charging timer works");
                print("The chargingTime value is" + chargingTimer);
            }
         }
          //what happens when it is charged
         else if(Input.GetButtonUp("Fire1"))
         {
            if(chargingTimer < 3)
            {
                //play animation
                Instantiate(basicProjectile, muzzle.transform.position, muzzle.transform.rotation);
                print("This is the basicProjectile");
                chargingTimer = 0;
            }
            else(chargingTimer > 3)
            {
                //play animation
                Instantiate(chargedProjectile, muzzle.transform.position, muzzle.transform.rotation);
                print("This is the chargedProjectile");
                chargingTimer = 0;
            }
         }
    }

Hi Well your code would not work because of some errors I could not find so I modified to this.

//inspector variables
var chargedProjectile 	: Transform;
var basicProjectile 	: Transform;
var muzzle 				: Transform;

var chargingTimer 		: float = 0;
var charged				: float = 3;

function Update()
{
	//chargingTimer is seconds dependant not frame.
	 //when the mouse button clicks charging begins
	 if(Input.GetButton("Fire1"))
	 {
	 	if(chargingTimer < charged)
	 	{
	 		chargingTimer += Time.time;
	 		Debug.Log (chargingTimer);
	 		print("The chargingTime value is" + chargingTimer);
	 	}
	 }

	 //what happens when it is charged
	else if(Input.GetButtonUp("Fire1"))
	 {
	 	if(chargingTimer < 3)
	 	{
	 		//play animation
	 		Instantiate(basicProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the basicProjectile");
	 		chargingTimer = 0;
	 	}
	 	else if(chargingTimer > 3)
	 	{
	 		//play animation
	 		Instantiate(chargedProjectile, muzzle.transform.position, muzzle.transform.rotation);
	 		print("This is the chargedProjectile");
	 		chargingTimer = 0;
	 	}
	 }
}

The console displays this when I use it the mouse button. when nothing is clicked however the console stays blank.

1270810--56475--$Capture.PNG

Apologies I forgot to change te Time.time to Time.deltaTime. AND IT WORKS!!! I WANT TO GIVE YOU LOTS AND LOTS OF COOKIES RIGHT NOW!!!

Virrors code looks correct. What errors did you get?

Despite probably wasting my time, ill provide another solution that’s slightly different.

var charging : bool;
var chargeTime : float;

function Update()
{
  if(Input.GetButtonDown("Fire1"))
    charging = true;

  if(Input.GetButtonUp("Fire1"))
    charging = false;

  if(charging)
    chargeTime += Time.deltaTime;
  else 
  {
     if(chargeTime > 3)
       FireProjectile(true);
     else if(chargeTime > 0)
       FireProjectile(false);
    
     chargeTime = 0;
  }
  
}

function FireProjectile(var powerful : bool)
{
  Debug.Log("Firing Projectile +" powerful);
}

Also in hindsight I am not really understanding how that worked but the one I did that was the same but had the late update didn’t work. Could you please explain how those changes made it work so I know for next time.

Sorry the code works in the post I placed after it. I offered lots of cookies. I am not to sure why it doesn’t and thanks for helping and giving your time anyway. It does not go unappreciated. Have a cookie too :slight_smile: