- Home /
Coding A Megaman-like Charge Shot?
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.
Answer by yaezah · Aug 19, 2017 at 06:47 PM
I'm still a noob so my code might be a little off, but try putting it in an update function and adding a bool to check if you can shoot or not so it doesn't shoot every frame, I sense that it's not shooting for you because it's only being called once in the start function..
public Transform chargeSpawn;
public GameObject chargeShot;
private float chargeTime = 0;
private float chargeRate = 2f;
public float fireRate1;
public float nextFire1;
public bool canShoot = false;
void Update ()
{
if (!canShoot) {
StartCoroutine (TimerRoutine ());
}
}
IEnumerator TimerRoutine()
{ canShoot = true;
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;
}
canshoot = false;
}
}
Oh my god, that's all it was! I made the mistake of using Start ins$$anonymous$$d of Update. It woks just fine now! Thanks, man. I really appreciate the help.
Glad it worked for you :) Probably didn't even require the bool either right? I just added it because I had the same problem yesterday but when I held mouse it would instantiate a prefab EVERY frame because it was in update xD
Actually, I did encounter another problem. It's fairly simple, though. Once the coroutine has finished, it doesn't continue to work after the first shot. Basically, the first one waits for the amount of time to pass before firing, but after that, they all fire as soon as the button is let go. I believe if I just manage to loop the coroutine that starts the timer, it should be solved. How should I go about making the coroutine repeat every time the button is held down?
Your answer
Follow this Question
Related Questions
Making Coroutine Repeat? 3 Answers
DmC: Devil May Cry Dual Shot? 0 Answers
Issues with bullet not subtracting properly 1 Answer
Problem with a charged projectile direction 1 Answer
Can't move and fire at the same time! 0 Answers