So, as the title implies, I’m trying to code a charged shot for a space shooting game I’m making for practice. Nothing special, just trying to set parameters to where the shot will only be fired if the button is held down for more than two seconds. Problem is, the shot won’t fire, and I don’t know what I did wrong. I tried to set it to a timer system, but so far, I haven’t had any luck. I separated the shot’s scripting into two parts: one merely controls the direction it goes as well as speed, which is only used to make the shot a prefab, and the other is supposed to define how it fires. Here is the script for firing the shot:
public Transform chargeSpawn;
public GameObject chargeShot;
private float chargeTime = 0;
private float chargeRate = 2f;
public float fireRate1;
public float nextFire1;
void Start ()
{
StartCoroutine(TimerRoutine());
}
IEnumerator TimerRoutine()
{
if (Input.GetButtonDown("Fire1"))
{
yield return new WaitForSeconds(2f);
chargeTime += chargeRate;
}
if (Input.GetButtonUp("Fire1") && Time.time > 2f)
{
Instantiate(chargeShot, chargeSpawn.transform.position, chargeSpawn.transform.rotation);
GetComponent<AudioSource>().Play();
chargeTime = 0;
}
if (Input.GetButtonUp("Fire1") && Time.time < 2f)
{
chargeTime = 0;
}
}
I’m sure it’s something simple I’ve overlooked, but, hey. And I’ve really not seen much discussion regarding charged shots. Hopefully someone else as green as I am can get some help out of this, too.