Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by WilibertXXIV · Apr 11, 2017 at 06:39 PM · shootingsetactiveshooterinstantiationobject pool

Problem with shooting multiple bullet at the same time with an Object Pooler

Top of the day to you all!

I've run into a frustrating end today. The problem here is that I've been using a simple code to shoot bullet in a fan fashion way with this code:

 private void Update()
 {
     if (Time.time > nextShot)
     {
         GetSpeed();
         nextShot = Time.time + fireRate;
         Quaternion qAngle = Quaternion.AngleAxis(numShots / 2 * spreadAngle, transform.up) * transform.rotation;
         Quaternion qDelta = Quaternion.AngleAxis(spreadAngle, transform.up);

         for (int i = 0; i < numShots; i++)
         {
             shot = Instantiate(shot, transform.position, qAngle);
             qAngle = qDelta * qAngle;
         }
     }
 }

Much much later I've built myself an Object Pool. I've changed all my instantiating code to set.Active. It works perfectly with everything else but with this code. This is the updated code for it to work with my Object Pool:

 void Update()
 {
     if (Input.GetButton("Fire1") && Time.time > nextFire)
     {
         nextFire = Time.time + fireRate;
         string name = shot.name;
         GameObject obj = ObjectPoolerScript.current.GetPooledObject(name);

         if (obj == null) return;

         Quaternion qAngle = Quaternion.AngleAxis(-numShots / 2 * spreadAngle, transform.up) * transform.rotation;
         Quaternion qDelta = Quaternion.AngleAxis(spreadAngle, transform.up);
        
         for (int i = 0; i < numShots; i++)
         {
             obj.transform.position = transform.position;
             obj.transform.rotation = qAngle;
             obj.SetActive(true);
             qAngle = qDelta * qAngle;
         }

The thing is; shots don't spread anymore and the command doesn't activate more than one bullet at a time (always at a specific location depending on the spreadAngle value).

I'm missing something here so if anyone of you have a clue about that let me know!

@FortisVenaliter Here is the object Pool script that I use:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class ObjectPoolItem
 {
     public GameObject objectToPool;
     public string poolName;
     //public string objectName;
     public int pooledAmount;
     public bool willGrow = true;
 }
 
 public class ObjectPoolerScript : MonoBehaviour
 {
     public const string DefaultRootObjectPoolName = "Pooled Objects";
 
     public static ObjectPoolerScript current;
     public string rootPoolName = DefaultRootObjectPoolName;
 
     public List<GameObject> pooledObjects;
     public List<ObjectPoolItem> itemsToPool;
     
     void Awake()
     {
         current = this;
     }
 
     private void Start ()
     {
         if (string.IsNullOrEmpty(rootPoolName))
             rootPoolName = DefaultRootObjectPoolName;
 
         GetParentPoolObject(rootPoolName);
         
         pooledObjects = new List<GameObject>();
         foreach (var item in itemsToPool)
         {
             for (int i = 0; i < item.pooledAmount; i++)
             {
                 CreatePooledObject(item);
             }
         }
     }
     
     private GameObject GetParentPoolObject(string objectPoolName)
     {
         // Use the root object pool name if no name was specified
         if (string.IsNullOrEmpty(objectPoolName))
             objectPoolName = rootPoolName;
 
         GameObject parentObject = GameObject.Find(objectPoolName);
 
         //Create the parent object
         if(parentObject == null)
         {
             parentObject = new GameObject();
             parentObject.name = objectPoolName;
 
             //Add sub pools to the root object po ol
             if (objectPoolName != rootPoolName)
                 parentObject.transform.parent = GameObject.Find(rootPoolName).transform;
         }
 
         return parentObject;
     }
 
     public GameObject GetPooledObject(string tag)
     {
         for (int i = 0; i < pooledObjects.Count; i++)
         {
             if (!pooledObjects[i].activeInHierarchy && pooledObjects[i].name == tag)
             {
                 return pooledObjects[i];
             }
         }
 
         foreach (var item in itemsToPool)
         {
             if (item.objectToPool.name == tag)
             {
                 if (item.willGrow)
                 {
                     return CreatePooledObject(item);
                 }
             }
         }
 
         return null;
     }
 
     private GameObject CreatePooledObject (ObjectPoolItem item)
     { 
         GameObject obj = Instantiate(item.objectToPool);
         
         //Get parent for this pooled object and assign the new object to it
         var parentPoolObject = GetParentPoolObject(item.poolName);
         obj.transform.parent = parentPoolObject.transform;
         obj.name = item.objectToPool.name;
         obj.SetActive(false);
         pooledObjects.Add(obj);
         return obj;
     }
 }


Have a good day!!

Comment
Add comment · Show 2
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 FortisVenaliter · Apr 11, 2017 at 06:41 PM 0
Share

Can you include the ObjectPoolerScript code?

avatar image WilibertXXIV · Apr 11, 2017 at 08:26 PM 0
Share

Yes I will as soon as I get home from work

1 Reply

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

Answer by WilibertXXIV · Apr 12, 2017 at 04:48 AM

It's finally working! It seems that using a Coroutine solved everything. I would still love to understand why the first code wouldn't/couldn't work properly!

Anyways, here's the script of the Coroutine:

 IEnumerator FanShot()
     {
         isStarted = true;
         while (isStarted == true)
         {
             Quaternion qAngle = Quaternion.AngleAxis(-numShots / 2 * spreadAngle, transform.up) * transform.rotation;
             Quaternion qDelta = Quaternion.AngleAxis(spreadAngle, transform.up);
 
             for (int i = 0; i < numShots; i++)
             {
                 string name = shot.name;
                 GameObject obj = ObjectPoolerScript.current.GetPooledObject(name);
                 obj.transform.position = transform.position;
                 obj.transform.rotation = qAngle;
                 obj.SetActive(true);
                 qAngle = qDelta * qAngle;
             }
             yield return new WaitForSeconds(nextFire);
         }
     }


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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Hi guys i have a character that gun dual wield guns, to make the gunshooting more natural i want the guns to not go of simultaneously. How do I make one gun have a slight delay? 0 Answers

2D Platfformer 0 Answers

Smoothly calculating a vector forward of the mid point between two vectors to allow two handed aiming in VR 0 Answers

2d shooting,Shooting bullets 2d 0 Answers

How the bullet does not pass through the object? 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