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 marksunara · May 25, 2013 at 08:37 PM · bulletgunprojectilemultipleweapon

Shooting multiple bullets

I have my character shooting bullets in the game. But I want to know how my character can shoot multiple bullets at once.

Here is the example: https://www.youtube.com/watch?v=-H2ka0kH71s

Can someone show how to make my character or weapon shoot multiple bullets at once, per click?

My Shooting code:

var playerProjectile: GameObject;

 function Update () {
  
     if(Input.GetKeyDown("space") && Time.time > nextShot){
  
        nextShot = Time.time * timeBetweenShots;
  
        Instantiate(playerProjectile, _transform.position, Quaternion.identity);
     }
  
 }
Comment
Add comment · Show 3
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 · May 25, 2013 at 08:46 PM 1
Share

For an accurate answer, please post your current shooting code.

avatar image Fattie · May 25, 2013 at 08:55 PM 1
Share

Invoke or InvokeRepeating for simple timers in unity. please first search here for 100s of excellent long examples or unityGems.com

avatar image robertbu · May 25, 2013 at 09:11 PM 0
Share

@Fattie - he is looking for how to create a 'fan' of bullet at each shot. I could not find a reference for him. I'll answer this when I have a bit more time if someone else does not get to it.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · May 25, 2013 at 11:20 PM

Here is some code that will shoot a fan shot:

 #pragma strict
 
 var playerProjectile: GameObject;
 var numShots = 5;            // Number of shots fired (should be odd); 
 var spreadAngle = 2.0;       // Angle between shots
 var timeBetweenShots = 0.5;  // Minimum time between shots
 
 private var nextShot = 0.0;
 
 function Start() {
   if (numShots / 2 * 2 == numShots) numShots++; // Need an odd number of shots
   if (numShots < 3) numShots = 3;  // At least 3 shots for a fan
 }
 
 function Update () {
     if(Input.GetKeyDown(KeyCode.Space) && Time.time > nextShot){
  
         nextShot = Time.time + timeBetweenShots;
         var qAngle = Quaternion.AngleAxis(-numShots / 2.0 * spreadAngle, transform.up) * transform.rotation;
         var qDelta = Quaternion.AngleAxis(spreadAngle, transform.up);
         
         for (var i = 0; i < numShots; i++) {
                 var go : GameObject = Instantiate(playerProjectile, transform.position,qAngle);
                 go.rigidbody.AddForce(go.transform.forward * 1000.0);
                 qAngle = qDelta * qAngle;
         }
     }
 }

There is one problem with this code. Since all the shots are Instantiated at the same location, they will collide with each other. You could save the game objects in an array and use Physics.IgnoreCollision() between all the pairs. An alternate solution is to start with the collider disabled and enabled it when the shots are free of each other. Here is a script:

 #pragma strict
 
 var radius = 0.051;
 
     function Update () {
       if (!collider.enabled && !Physics.CheckSphere(transform.position, radius)) {
         collider.enabled = true;
         }
     }

'radius' is the radius of a sphere that could contain the projectile. The only issue with this solution is that the bullets will not start hitting anything until they are not touching. That means they will not hit anything at point-blank range.

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 FL · May 25, 2013 at 10:23 PM

Just make a prefab with multiples bullets. You probably need to ignore the collisions between the bullets, so use Physics.IgnoreLayerCollision(bulletLayerNumber,bulletLayerNumber,true) when the scene is loaded. Don't forget to create a new layer for 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

14 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

Related Questions

Could you advice me why this Gun and Ballistic Script doesn't work? 1 Answer

Make a Gun fire multiple times 3 Answers

Help with getting a bullet to move. 2 Answers

Bullet Always fires down the Z Axis using Bullet Drop/Trajectory Formula? 1 Answer

Bullet Penetration 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