Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Funlamb · Jan 23, 2015 at 04:26 AM · c#poolingobject pool

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattyman174 · Jan 23, 2015 at 04:53 AM 0
Share

Show the code for RapidFire() $$anonymous$$ethod.

$$anonymous$$y Skype: aatdevilsdemon

avatar image Funlamb · Jan 23, 2015 at 06:25 AM 0
Share

I added the code. Thanks for willing to skype. When is a good time? I can do Saturday if you're free.

avatar image mattyman174 · Jan 23, 2015 at 06:41 AM 0
Share

Just add me and we can work it out.

avatar image hav_ngs_ru · Jan 23, 2015 at 02:02 PM 0
Share

rapidFireTime == ?

avatar image Funlamb · Jan 23, 2015 at 03:19 PM 0
Share
 rapidFireTime = 0.1 

Every update rapidFireTimeCurrent gets reduced. Once rapidFireTimeCurrent is below 0.0f it allows the player to fire again.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges