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 Kalbytron · Feb 25, 2014 at 04:16 AM · limitbullets

Fire fixed number of bullets

I am firing a cannon that fires bullets, but I would like to limit the number of bullets on screen per player, just like in megaman when you can only have 3 bullets on the screen at a time. Anyone care to share a pear? (A example code would be nice)

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 robertbu · Feb 25, 2014 at 04:24 AM 0
Share

The solution will depend on your firing code, how you have your game structured, and how you want to define the lifespan of the bullets. Are you reusing bullets are are you destroying and instantiating new ones? Do you literally want to detect if the bullet is still visible, or do you want to give each bullet a lifespan? Posting your firing code would help.

avatar image Kalbytron · Feb 25, 2014 at 05:48 AM 0
Share

I want to detect if the bullets from a player are still in the world, so if 1 of the 3 fired bullets gets destroyed, I can immediately fire another.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Feb 25, 2014 at 05:54 AM

I want to detect if the bullets from a player are still in the world

One solution is to give your bullets a tag that is only used for bullets. Say you use 'Bullet' and are firing on a mouse click, you can do:

 var countOfExistingBullets = GameObject.FindGameObjectsWithTag("Bullet").Length;
 if (countOfExistingBullets < 3 && Input.GetMouseButtonDown(0)) {
     // Firing code goes here
 }
Comment
Add comment · Show 3 · 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 nesis · Feb 25, 2014 at 06:03 AM 0
Share

Also, it should go without saying, but just in case - you'll want to destroy your bullets when they hit solid geometry. So in the script attached to your bullet's GameObject (assu$$anonymous$$g your bullet has a collider):

 function OnCollisionEnter(other:Collider) {
     //this line will let you make other GameObjects react to
     //being hit by a bullet as long as they have a collider,
     //are on the right layer to collide with bullets, and
     //have a function called TryDamage(int damageAmount)
     other.Send$$anonymous$$essage("TryDamage",1,Send$$anonymous$$essageOptions.DontRequireReceiver);
     //maybe spawn some bullet-explosion-on-impact prefab here
     Destroy(gameObject);
 }
avatar image Kalbytron · Feb 25, 2014 at 06:03 AM 0
Share

Alright, testing code!

avatar image Kalbytron · Feb 25, 2014 at 09:15 PM 0
Share

ALRIGHT, the code has worked. Also, I implemented it so that it works with multiplayers. Thanks

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

Shooting with Limited Ammo 2 Answers

Dividing Extruded Object into Manageable Pieces? 0 Answers

65,000 vertices or 65,000 triangles? Disambiguation please. 2 Answers

Creating limits on how far something can rotate. 1 Answer

Limit rotation of weapons with mouse help 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