Hey all, I’m a bit new to programming so sorry if this is a noobish question to ask, however I’m creating a space shooter game and am trying to add power-ups into the game. I’ve implemented a power-up where the firing rate would increase for a set amount of time however,one problem that is happening for me is that since the user will be holding down the spacebar to fire(Input.GetKeyDown(KeyCode.Space)), once they get the upgrade for some reason the firing rate won’t change unless you let go of the key and press it again. My question is, how exactly would I be able to make it so that the firing rate of the ship will change automatically once the user gets the upgrade? I tried changing the GetKeyDown method, moved the LaserPower up shotRate statement to the update function, and tried other approaches, but neither have worked for me. Does anyone have any suggestions on how to solve this?
Here’s the Player Script that’s attached to the play ship:
public static float FiringRate = 0.2f;
voidUpdate () {
if (Input.GetKeyDown (KeyCode.Space))
InvokeRepeating ("FireLaser", 0.000001f, FiringRate);
}
if (Input.GetKeyUp (KeyCode.Space)) {
CancelInvoke ("FireLaser");
}
voidOnTriggerEnter2D(Collider2Dcollider){
Lasershot = collider.gameObject.GetComponent<Laser> ();
LaserPowerUpshotRate = collider.gameObject.GetComponent<LaserPowerUp> ();
if (shotRate) {
Debug.Log ("LASER INCREASED!");
shotRate.shotSpeed();
}
}
And the LaserPowerup Script:
public void shotSpeed(){
Debug.Log ("I'm HERE");
PlayerScript.FiringRate = 0.09f;
Destroy (gameObject);
}
}
Thanks!