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 Julac · Jan 24, 2017 at 08:09 AM · physics3dprojectiletopdownobject pool

Why are my projectiles spawning in batches?

My game is a 3D top-down space shooter. I have an object pooler that is holding the ship's projectile and scripts that activate/deactivate, retrieve them, and apply force to them when pulling the trigger of a controller.

What I can't get to happen is for the game to let me cycle through my whole pool. I want to be able to hold the trigger down and have it fire bolts until the pool drains or. What it keeps doing is releasing 3-8 bolts per trigger pull every time. My pool never gets exhausted and I never get nice, evenly spaced bolts. I've tried a few different rate of fire techniques from the internet and a few of my own without any success. At best I can get it to evenly space 5-6 bolts per trigger hold, but inevitably it stops firing bolts well before the pool is empty. It's also never a consistent amount of bolts before it stops shooting and some times there is a long pause (of a length I know I didn't set any where) before I can shoot again.

I'll attach my code and a video of what it's doing below.

https://youtu.be/G2s7vYeZhJM

The script controlling the player (handles ship movement and weapon firing), the commented out code are different methods I've tried, not necessarily relevant to this issue.

CSharp

 public class PlayerController : MonoBehaviour {
 
  public float agility;
  public float speed;
  public float tilt;
  public float fireRate;
  public float projectileForce;  
  public Rigidbody rb;
 
  private GameObject BS;
 
  private float nextFire = 0;
  GameObject tempObj;
  void Start()
  {
     BS = GameObject.FindGameObjectWithTag("weapon");
     rb = GetComponent<Rigidbody>();
  }
 
  void Fire()
  {
      Debug.Log("fire called");
      GameObject obj = BS.GetComponent<ObjectPoolerWeapon>().GetPooledObject();
      Rigidbody rb;
      rb = obj.GetComponent<Rigidbody>();
 
 
      obj.transform.position = BS.transform.position;
      obj.transform.rotation = BS.transform.rotation;
      obj.SetActive(true);
      rb.AddForce(transform.forward * projectileForce);
  }
 
 void Update() { nextFire = Time.time + nextFire;
 
  }
  void FixedUpdate()
  {
      if ((Input.GetAxis("PrimaryAttack") > 0) && Time.time < nextFire)
      {
 
          Fire();
 
      }
 
      float moveHorizontal = Input.GetAxis("HorizontalJ");
      float moveVertical = Input.GetAxis("VerticalJ");
      float y = Input.GetAxis("RightH");
      float nextShot;
   
     // Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
      rb.transform.Rotate(0, (y * agility), 0);
      if (moveHorizontal  > 0)
      {
          rb.AddForce(transform.right * speed);
      }
      if (moveHorizontal < 0)
      {
          rb.AddForce((transform.right * -1) * speed);
      }
      if (moveVertical >0)
      {
          rb.AddForce((transform.forward * speed) * moveVertical);
      }
      if (moveVertical < 0)
      {
          float reverseSpeed;
          reverseSpeed = ((speed) * -1);
          rb.AddForce(transform.forward * reverseSpeed);
      }
    //  rb.MovePosition(transform.position + transform.forward * (moveVertical * speed));
      rb.position = new Vector3
          (
                 GetComponent<Rigidbody>().position.x,
                 10.0f,
                 GetComponent<Rigidbody>().position.z
          );
      // GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
 
    
  }
 
   
 }

The object pooler I'm using

CSharp

 public class ObjectPoolerWeapon : MonoBehaviour
 {
 
     public GameObject pooledObject;
     public int pooledAmount = 20;
     public bool willGrow = true;
     public GameObject weapon; //represents the object that this script is attached to
 
     private GameObject obj;
     private float nextFire;
 
     List<GameObject> pooledObjects;
 
 
     void Start()
     {
         pooledObjects = new List<GameObject>();
 
         for (int i = 0; i < pooledAmount; i++)
         {
             GameObject obj = (GameObject)Instantiate(pooledObject);
             obj.SetActive(false);
             pooledObjects.Add(obj);
         }
     }
 
     public GameObject GetPooledObject()
     {
         for (int i = 0; i < pooledObjects.Count; i++)
         {
             if (!pooledObjects[i].activeInHierarchy)
             {
                 return pooledObjects[i];
             }
         }
 
         if (willGrow)
         {
             GameObject obj = (GameObject)Instantiate(pooledObject);
             pooledObjects.Add(obj);
             return obj;
         }
 
         return null;
     }
 
 }
 ]public class ObjectPoolerWeapon : MonoBehaviour
 {
 
     public GameObject pooledObject;
     public int pooledAmount = 20;
     public bool willGrow = true;
     public GameObject weapon; //represents the object that this script is attached to
 
     private GameObject obj;
     private float nextFire;
 
     List<GameObject> pooledObjects;
 
 
     void Start()
     {
         pooledObjects = new List<GameObject>();
 
         for (int i = 0; i < pooledAmount; i++)
         {
             GameObject obj = (GameObject)Instantiate(pooledObject);
             obj.SetActive(false);
             pooledObjects.Add(obj);
         }
     }
 
     public GameObject GetPooledObject()
     {
         for (int i = 0; i < pooledObjects.Count; i++)
         {
             if (!pooledObjects[i].activeInHierarchy)
             {
                 return pooledObjects[i];
             }
         }
 
         if (willGrow)
         {
             GameObject obj = (GameObject)Instantiate(pooledObject);
             pooledObjects.Add(obj);
             return obj;
         }
 
         return null;
     }
 
 }[/code]



And finally on a side note besides this issue, any advice on how I can get the bolts to travel at the same speed/direction as the ship and then ADD more force so they move ahead of the moving ship? I spent a good 6 hours trying things this past weekend and got no where. The end of the video shows the exact behavior I'd like to avoid.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PizzaPie · Jan 24, 2017 at 09:20 PM

The problem is on the

 void Update() {
  nextFire = Time.time + nextFire;     //1st problem
   }
   void FixedUpdate()
   {
       if ((Input.GetAxis("PrimaryAttack") > 0) && Time.time < nextFire)     //2nd
       {
  
           Fire();
  
       }

the way you have it Update() is called before fixedUpdate so nextFire is always >Time.time is always true.

 float fireRate;
 void Start(){ 
 nextFire = Time.time;
 }
 void FixedUpdate()
       {
           if ((Input.GetAxis("PrimaryAttack") > 0) && Time.time > nextFire)     
           {
               Fire();
 nextFire = Time.time + fireRate;
               }

Set fireRate to some number and remove the Update(), it should work. On GetPooledObject() it seems you have the willGrow always set on true so it will always instatiate more shots when all current shots are active. Now if willGrow= false then Fire() should throw some errors regarding that the obj you pull with the GetPooledObject might be null so you should add after the line you set the obj that

 if(obj== null)
 return;

Hope that helps.

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

128 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 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 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 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 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 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 avatar image avatar image

Related Questions

RigidBody.AddForce() doesn't apply 2 Answers

Unity Faux gravity on a cube: reaching some faces is troublesome 0 Answers

Everything is appearing behind camera background 0 Answers

How to make a wall passable if you are not looking at it? 0 Answers

how to stand up a ragdoll 0 Answers


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