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 /
avatar image
0
Question by JoeDerpz · Dec 11, 2017 at 10:10 PM · optimizationinstantiationobject pool

Shooting using an object pool

This is actually my first time posting a question in Unity Answers so if this question has already been asked please excuse me.

Today I am working on a gun that shoots at center screen and uses an object pool, this should be pretty simple but for whatever reason I am having some issues. I have a script on the player called Gun.CS when it uses instantiation the bullets trajectory is perfect but when I incorporate the object pool the bullets do not shoot correctly anymore. Any help would be greatly appreciated!''

  // Update is called once per frame
         void Update () 
         {
             Aim();
             if (Input.GetButtonDown ("Fire2") && Time.time > nextFireTime && currAmmo > 0) 
             {
                 Shoot();
             }
         }
     
         void Shoot()
         {
             print("firing");
             audio.PlayOneShot(impact, volume);
    
 
           //This is how I would like to fire my bullets, but they fly in different directions
 
             Rigidbody cloneRb = GenericObjectPool.current.GetPooledObject("BulletBlue").GetComponent<Rigidbody>();
             cloneRb.gameObject.SetActive(true);
             cloneRb.gameObject.transform.position = firePoint.position;
             cloneRb.gameObject.transform.rotation = firePoint.rotation;
 
                 //This currently does work but I do not want to create and destroy bullets as this will probably be a mobile game
             //GameObject go =  Instantiate (projectile, firePoint.position, firePoint.rotation);// as Rigidbody;
             //go.GetComponent<Rigidbody>().AddForce(firePoint.transform.forward * projectileForce);
             currAmmo -= 1;
             nextFireTime = Time.time + fireRate;
         }
     
         void Aim()
         {
             float screenX = Screen.width / 2;
             float screenY = Screen.height / 2;
             RaycastHit hit;
             ray = cam.ScreenPointToRay(new Vector3(screenX,screenY));
     
             if (Physics.Raycast(ray, out hit))
             {
                 firePoint.LookAt(hit.point);
             }
         }
 
 
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 Minuks · Dec 12, 2017 at 01:31 AM 0
Share

3 questions and 1 lead:
- Why do you set the Rigidbody active and not the whole gameObject ?
- Also why is //go.GetComponent().AddForce(firePoint.transform.forward * projectileForce); commented ?
- Finally, what do you mean with "do not shoot correctly" ?

$$anonymous$$aybe you could try setting the rigidbody variables to 0 when you take your object out of the pool ? (like the velocity)

avatar image sethuraj · Dec 12, 2017 at 09:30 AM 0
Share

Hi, Though the bullet gameobject gets deactivated by pool manager, its velocity wont get reset. So I think once the bullet gameobject gets deactivated, you need to reset its velocity for next launch.

 //Attach this script to the bullet prefab
 public class Bullet : $$anonymous$$onoBehaviour
 {
 
     //Function will be called when this gameobject gets deactivated
     void OnDisable()
     {
         //Reset the velocity of the rigidbody component
         gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

78 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

Related Questions

Object pulling prefabs that are randomly generating between random points? 0 Answers

New Object Pooling Problem 1 Answer

Is Instantiating bullets/many objects always bad for performance? 1 Answer

When should I use object pool? 2 Answers

What is best and optimized Technique of level Handling in Parking game ? loading from resources or enable in hierarchy 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