• 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 Freak-Boi · yesterday · shooting

how to fire with limited bullet ?

here is my code i just need to fire only 3 bullets but i dont understand how to do it

public Transform[] firepoint; public Transform firepoints; public Transform Firepoint; public GameObject bulletPrefab; public GameObject RocketPrefab; public GameObject GernadePrefab; public GameObject LandMine; private int currentBullets = 0; private int totalbullets = 4;

 public float BulletForce = 20f;
 public float GernadeForce = 20f;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame 
 void Update()
 {
     if (GameManager.Instance.isGameInPlay == true)
     {
             if (Input.GetKeyDown(KeyCode.LeftControl))
             {
                 if (CarController.instance.CurrentWeapon == 1)
                 {
                     Shoot();
                 }
                 if (CarController.instance.CurrentWeapon == 2)
                 {
                     BulletShoot();
                 }

                 if (CarController.instance.CurrentWeapon == 3)
                 {
                     Gernade();
                 }

                 if (CarController.instance.CurrentWeapon == 4)
                 {
                     Landmine();
                 }
             }
     }
     
 }
 void BulletShoot()   
 {
     if (currentBullets > 0)
     {
         foreach (var firepoint in firepoint)
         {
             GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
             Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
             rb.AddForce(firepoint.up * BulletForce, ForceMode2D.Impulse);
         }
         

     }
     else
     {
         currentBullets = totalbullets;
     }
 }
 void Shoot()
 {
   

         GameObject bullet = Instantiate(RocketPrefab, firepoints.position, firepoints.rotation);
         Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
         rb.AddForce(firepoints.up * BulletForce, ForceMode2D.Impulse);
     
 }

 void Gernade()
 {
     GameObject bullet = Instantiate(GernadePrefab, firepoints.position, firepoints.rotation);
     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
     rb.AddForce(firepoints.up * GernadeForce, ForceMode2D.Impulse);
 }
 
 void Landmine()
 {
     GameObject bullet = Instantiate(LandMine, Firepoint.position, Firepoint.rotation);
     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
     rb.AddForce(Firepoint.up * GernadeForce, ForceMode2D.Impulse);
 }
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

3 Replies

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

Answer by Deuce2008 · yesterday

If you mean fire multiple bullets, then you should make a

  public int bulletNumber

When button is pressed loop through that many times:

 for(int i = 0; i < bulletNumber; i++)
 {
     //Your firing code here
 }
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 Freak-Boi · 15 hours ago 0
Share

actually, I want 3 bullets only fire but I didn't know how to do it in the script

avatar image
0

Answer by Hayley845 · 13 hours ago

A bullet isn't likely to go off when you drop the cartridge for various reasons, including how it lands. Most dropped bullets land tip first on a floor or the ground. When this happens, the bullet will land in a way that prevents the impact from being sufficiently forceful to cause the bullet to fire.

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 Idual · 7 hours ago

I'm not sure exactly what you are trying to achieve but I suspect that you want the bullets to fire sequentially.

In your bulletShoot() method you need to test if the number of bullets fired is greater than or equal to the number you want to allow.

Something like this:

 if(currentBullets < 3) 
 {
      /* code to fire your bullet */
      currentBullets++;
 }

You could then decrease currentBullets by one when a bullet hits it's target or exceeds it's max range.

You might also want to add a time delay between the bullets. This can be done like this.

  if(currentBullets < 3 && Time.time > lastFired+1) 
     {
          /* code to fire your bullet */
          currentBullets++;
          lastFired=Time.time;
     }

This will give a delay of one second between bullets.

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

142 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

Related Questions

GUI Joystick 1 Answer

How to make a gun in unity? 1 Answer

Shooting a projectile the longer a button is held c# 1 Answer

Finding point of mesh visible by another game object 1 Answer

Shoot when Xbox 1 trigger pressed. 0 Answers


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