- Home /
Object pool help (All bullets fire at once)
Hey guys and gals, I need some help with bullets I have pooled into an objectpool. The problem I'm having is on the first push of the fire button all the bullets get fired at once. Once they are off the screen and are ready to be fires again they fire correctly. I've added my code. Let me know if you need any more of it.
MachineGun.cs:
void Update () {
RapidFire();// This is a timer that makes it so only the player can only fire 10 bullets a second
float trigger = Input.GetAxis ("Trigger");
if (trigger > 0.5f || Input.GetButton("Fire1")) {
rapidFireBullet = true;
} else {
rapidFireBullet = false;
}
}
void FireBullet () {//If the RapidFire() allows for another bullet and rapidFireBullet is true this method gets executed.
fire1 = _ship._objectPools.bullets.GetPooledObject();// This is where I fill grab a bullet with ObjectPool.cs
if (fire1 != null){
fire1.transform.position = gunPoint.transform.position;
float zdiff = Random.Range(-accuracyFire,accuracyFire);
fire1.transform.Rotate( new Vector3 (0,0,zdiff));
fire1.GetComponent<Projectile>().FireMe();
}
}
void RapidFire() {
if (rapidfireTimeCurrent > rapidFireTimeCap && rapidFireBullet) {
FireBullet();
rapidfireTimeCurrent = 0.0f;
}
rapidfireTimeCurrent -= Time.deltaTime;
}
ObjectPool.cs:
public GameObject GetPooledObject() {
for (int i = 0; i < _pooledObjects.Count; i++) {
if (!_pooledObjects[i].activeInHierarchy) {
return _pooledObjects[i];
}
}
I've been working on this for the last few hours. Any help would be great but what I would really like is if one of you could skype and screen share with me. I'd love to get a code review of this to make sure I'm at least doing this well. Let me know if you can give me about 30 minutes to an hour and maybe I can actually become a good programmer. -Thanks
Show the code for RapidFire() $$anonymous$$ethod.
$$anonymous$$y Skype: aatdevilsdemon
I added the code. Thanks for willing to skype. When is a good time? I can do Saturday if you're free.
rapidFireTime = 0.1
Every update rapidFireTimeCurrent gets reduced. Once rapidFireTimeCurrent is below 0.0f it allows the player to fire again.
Answer by Funlamb · Jan 24, 2015 at 08:03 PM
void RapidFire() {
if (rapidfireTimeCurrent > rapidFireTimeCap && rapidFireBullet) {
rapidfireTimeCurrent = 0.0f;//<<<<======== Switch this
FireBullet();//<<<<======== and this
}
rapidfireTimeCurrent += Time.deltaTime;
}
I switched the order of rapidFireTimeCurrent = 0.0f and FireBullet() Before this change the FireBullet() would execute until it ran out of bullets in the list.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Optimising for mobile - Instantiate 1 Answer
Moving object top to down 2 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer