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 vinodkumar · Jun 16, 2012 at 09:36 AM · instantiatebullet

problem with instantiate function

i am having a problem with instantiating bullet in my project. when i call the fire then it instantiate three bullet at a time. i don't know what is the problem. please someone help me.

the code is:

 function Fire () {
     // Did the time exceed the reload time?
     if (Time.time > reloadTime + lastShot && ammoCount > 0 && count>0) {
         count--;
         if(count <= 0){
         
             yield WaitForSeconds(2.0);
             count = resetcount;
             Debug.Log("print");
     
         }
         
         //guitexthint.message = count;
             
         // create a new projectile, use the same position and rotation as the Launcher.
         var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
             
         // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
         instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

         // Ignore collisions between the missile and the character controller
         Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
         
         lastShot = Time.time;
         ammoCount--;
     }
 
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Wolfram · Jun 16, 2012 at 01:51 PM

The code you presented will only spawn at most one projectile for each call to Fire(). So either there is a problem in your calling function, calling Fire() more than once, or it is a delayed reaction:

If count is

However, this problem depends on the value of "resetcount" (and the initial value of "count"). If it is 1, one call to Fire() (assuming you have ammo and realoadTime has passed) will be delayed by 2 seconds, then one shot will be fired. Calling Fire() again in the meantime will do nothing. If it is 2, one call to Fire() will fire immediately, and then the same happens as with 1, and so on.

What is your intention of using this counter named "count" and delaying the shot by two seconds?

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

Answer by vinodkumar · Jun 18, 2012 at 07:11 AM

thanks for your reply . i have set the vales of resetcount and count to 30, so that after every 30 bullet it will wait for 2 seconds. but i don't know why it is firing more than one projectile/bullet on every call to the fire function

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 Wolfram · Jun 18, 2012 at 05:02 PM 0
Share

Please don't add comments as answers, there is an "add new comment" button for that.

As I told you in my answer, it is not possible for a single call to Fire() to spawn multiple particles. So if that is the effect you see, the problem is in the calling script, i.e., every place where you call Fire(), or maybe you have the script(s) that calls Fire() attached more than once, or attached to multiple objects.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Gun Projectile Shooting In Wrong Direction (Javascript) 1 Answer

How to check who shot whom 3 Answers

Instantiating a pattern in front of the player? 1 Answer

Rotation and Velocity of bullets. 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