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;
}
}
}
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.
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;
}
}
}
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?
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