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 PinkMustard · Jun 09, 2015 at 11:54 PM · instantiatebulletfire rateuneven

Uneven firerate

Hello. I noticed a weird thing that is happening when i try to shoot some bullets. It appears that the bullets are not getting instantiated at an even rate (see attached picture 1)

The first attached picture is a screenshot of a simple test scene. The code used in this scene for the instantiation is:

     public GameObject cube;
     public float fireRate = 0.075f;
     
     void Start () {
         Fire();
     }
 
     void Fire(){
         Instantiate(cube, transform.position, Quaternion.identity);
         Invoke("Fire", fireRate);
     }


And for moving the bullets:

     void Update () {
         transform.Translate(0, 4 * Time.deltaTime, 0);
     }

The second attached picture shows a SHMUP game that I'm working on. It's the same concept but the fire mechanics utilizes the ObjectPooling script (taken from the Live Training Session) instead.

Q: Why are the bullets spawning with an uneven rate when I increase the fire rate? Is Unity unable to handle this? Have I coded something funky?

This looks really ugly and I don't know how to fix it, any help would greatly be appreciated!

Thanks in advance

[1]: /storage/temp/47939-1.png

[2]: /storage/temp/47940-2.png

1.png (12.6 kB)
2.png (8.9 kB)
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 DiegoSLTS · Jun 10, 2015 at 12:56 AM

"Invoke("Fire", fireRate);" will execute the "Fire" method after fireRate seconds, counting from the moment that line was executed. Before calling that line you Instantiate an object, that operation takes time. If you make fireRate too small, the Instantiate line might be delaying the Invoke call a significant amount (the smaller the fireRate, the bigger the impact of the delay).

I'm not sure why you did that kind of "recursive" call to create fires, it's like a fire creates the next, instead of a weapon creating them.

Why not do something like this:

  1. There should be a line of code that starts the first fire, right? Go to that line.

  2. Instead of Instantiating one fire and letting the rest instantiate themself, write a function that instantiates the fire, and call that function repeatedly. Something like this:

    //instead of Instantiating the first fire... InvokeRepeating("Fire",fireRate,fireRate);

    //In the same script add this function void Fire() { Instantiate(cube, transform.position, Quaternion.identity); }

This way each invokation will be at the same fireRate, and the Instantiate will ocurr during that time between fires.

Comment
Add comment · Show 1 · 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
avatar image PinkMustard · Jun 10, 2015 at 04:50 PM 0
Share

Thanks for your answer, I appreciate it

I have tried this implementation, but it yields the same result. The Shooting script for my space ship looks like this at the moment:

     void Start () {
         objectPooler = GetComponent<ObjectPooler>();
             //Repeats the invoke, but yields the same result
         InvokeRepeating("Fire", fireDelay, firerate);
     }
     
 
 
     private void Fire(){
             //Gets a bullet from an object pooler
         GameObject obj = objectPooler.GetPooledObject();
 
         if(obj == null) return;
         
         obj.transform.position = transform.position;
         obj.transform.rotation = transform.rotation;
         obj.SetActive(true);
 
 
     }

I figured that the Instantiation would take time. This implementation however makes it so that all bullets gets instantiated under the Start method once. Every bullet is then ready to be fired once the "Fire" method gets called for the first time. When using this implementation with an object pooler, the bullets are never destroyed or instantiated, thus it can not delay the repeating invoke call.

Any other ideas? Thanks again.

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

Bullets won't fire? 0 Answers

Cannot destroy instantiated objects 2 Answers

Trying to make an enemy fire at the player 1 Answer

Bullet always facing the sky. 3 Answers

bullet issue 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