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 Denzel_38 · Jan 20, 2015 at 01:30 PM · bullets

Firing different bullets

I'm working on some small project here. What I basically want to do is when a key is pressed the player changes the bullets he fires. Now I know how to fire bullets. But the changing of the bullets when key is pressed is something I cant figure out. I would love to get some guidance or be pushed into the right direction so I can make this.

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 DevsGoingViral · Jan 20, 2015 at 02:15 PM 0
Share

Did you tried "switch" statement and then "case 1","case 2"? Here is good link for "switch" statement. Link

avatar image El Maxo · Jan 20, 2015 at 02:33 PM 0
Share

how are you firing these bullets, could we see your code?

2 Replies

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

Answer by nightowl79a · Jan 20, 2015 at 02:16 PM

What I would do is make a array or list of prefabs for the bullets. Then I would make an int variable(curBullet for exsample) to store the current bullet. When the player presses the change weapon button he would change the curBullet variable, and I would instatiate the bullet matching the position in the array. Here is what the code would look like if that was a bit confusing:

 public class FireBullets : MonoBehaviour {
     public GameObject[] bullets;
     int curBullet = 0;
 
     void Update () 
     {
         if (Input.GetKey(KeyCode.Tab))
         {
             // Add one to the curBullet variable when the player presses tab. 
             //This will allow you to fire the next bullet in the bullets Array
             curBullet++;
             // If the curBullet is higher than the bullet array length set it back to 0
             if (curBullet > bullets.Length)
             {
                 curBullet = 0;
             }
         }
         if (Input.GetButton("Fire1"))
         {
             // Fire the bullet matching is position in the array of bullets
             Instantiate(bullets[curBullet], transform.position, transform.rotation);
         }
     }
 }

 
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 sniper43 · Jan 20, 2015 at 02:29 PM

Are you using a line renderer? In which case you should change the material/texture/hue.

If you are generating new objects for eact bullet, just change the type of bullet being instantiated by having a GameObject that gets spawned and using your listener to adjust what that object is.

Example: using UnityEngine; using System.Collections;

 class asd
 {
     public GameObject bullet;
 
     void Update()
     {
         if( Input.getButton( "valueForFiring" ) )
             Instantiate(bullet);
     }
 
 
     public void changeBullet( GameObject newBullet)
     {
         bullet = newBullet;
     }
 }
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting with Limited Ammo 2 Answers

Need hep with pooling(call Cs in Js) 0 Answers

Getting Bullets to fire in Direction of Gun Barrel 1 Answer

How do I make bullets go through my collider? 1 Answer

Need some help with bullets spread fire!!! 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