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 hamo0d · May 11 at 03:20 PM · unity 2d

object pooling shoots all bullets at once

Im having trouble with object pooling. whenever i try shooting it shoots all the bullets at once instead of one at a time. any help? this is the code for the bullet:

public class Projectile : MonoBehaviour

{

 [SerializeField] private float speed;
 private float direction;
 private bool hit;
 private float lifetime;

 private Animator anim;
 private BoxCollider2D boxCollider;

 private void Awake()
 {
     anim = GetComponent<Animator>();
     boxCollider = GetComponent<BoxCollider2D>();
 }
 private void Update()
 {
     if (hit) return;
     float movementSpeed = speed * Time.deltaTime * direction;
     transform.Translate(movementSpeed, 0, 0);

     lifetime += Time.deltaTime;
     if (lifetime > 5) gameObject.SetActive(false);
 }
 private void OnTriggerEnter2D(Collider2D collision)
 {
     hit = true;
     boxCollider.enabled = false;
     anim.SetTrigger("Explode");
 }
 public void SetDirection(float _direction)
 {
     lifetime = 0;
     direction = _direction;
     gameObject.SetActive(true);
     hit = false;
     boxCollider.enabled = true;

     float localScaleX = transform.localScale.x;
     if (Mathf.Sign(localScaleX) != _direction)
         localScaleX = -localScaleX;

     transform.localScale = new Vector3(localScaleX, transform.localScale.y, transform.localScale.z);
 }
 private void Deactivate()
 {
     gameObject.SetActive(false);
 }

}

and this is the code for the shooting:

public class Attacks : MonoBehaviour

{

 private Movement movement;
 [SerializeField] private float attackCooldown;
 [SerializeField] private Transform gunPoint;
 [SerializeField] private GameObject[] Bullet;

 private Animator anim;
 private Movement playerMovement;
 private float cooldownTimer = Mathf.Infinity;

 private void Awake()
 {
     anim = GetComponent<Animator>();
     playerMovement = GetComponent<Movement>();
 }

 private void Update()
 {
     if (Input.GetMouseButton(0))
         Attack();

     cooldownTimer += Time.deltaTime;
 }

 private void Attack()
 {
     
     cooldownTimer = 1;

     Bullet[FindFireball()].transform.position = gunPoint.position;
     Bullet[FindFireball()].GetComponent<Projectile>().SetDirection(Mathf.Sign(transform.localScale.x));
 }
 private int FindFireball()
 {
     for (int i = 0; i < Bullet.Length; i++)
     {
         if (!Bullet[i].activeInHierarchy)
             return i;
     }
     return 0;
 }

}

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 Captain_Pineapple · May 12 at 09:40 AM

Hey there,

in general this issue is probably not really connected to your current poolign approach but instead to be found here:

   if (Input.GetMouseButton(0))

This function checks if the mouse button is down.

Assuming you are running 60 Updates a second this will check if the mouse button is down every 1/60 seconds. So unless you are reallyreally quick this will always trigger multiple times per click.

What you want to do instead is

  if (Input.GetMouseButtonDown(0))

Which will only trigger once per click.


As i see you wanted to solve this with a cooldown timer but that does not do anything related to fireing yet?


Apart from that an improvement suggestion if you change your current code to:

  var BulletID = FindFireball();
  Bullet[BulletID].transform.position = gunPoint.position;
  Bullet[BulletID].GetComponent<Projectile>().SetDirection(Mathf.Sign(transform.localScale.x));

As this is way friendlier on performance.


Apart from this i'd also suggest that you make your object pooling more variable:

  • use a List<GameObject> Bullets instead of an array.

  • if add a Prefab for your Bullet

  • if your FindFireBall function does not find a deactivated one -> have it instantiate a new one and add it to the List.

Why might this be better? Because down the road when more and more entities in a scene might find a fireball this could lead to a case where the fireball with the ID zero is constantly taken from its current location and respawned somewhere else.

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

150 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 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

Only sideways colliding detection 2 Answers

Second Coroutine isn't working Unity 1 Answer

how to stop rotation from being uncontrollable 1 Answer

Unity UI - layout fixed number of buttons 1 Answer

How can i save a variable for only one scene? 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