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 CJGames · Dec 11, 2014 at 03:28 PM · fpssystemweaponhow tofirst person shooter

How to do Switching Weapon System

Well i'm making an FPS and blah blah...I need more than 1 gun, so, what's the easiest way to do a weapon switching system (C# or JavaScript)? I have my Player Like this: Player > Main Camera > PrimaryWep > M4; Bullet-FirePoint; And my Gun script(Attatched to PrimaryWep, M4 is just the 3D model) works like this:

 #pragma strict
 //Weapon Script
 
 var bullet : GameObject;
 var gunBarrel : Transform;
 var reloadSound : AudioClip;
 var shootSound : AudioClip;
 var emptySound : AudioClip;
 var hudAmmo : GUIText;
 var hudMags : GUIText;
 
 var ammo : int;
 static var mags : int;
 var fireRate : boolean;
 var shootTime : float;
 
 var aimingDownSights : boolean = false;
 
 function Start () {
 
     ammo = 45;
     mags = 225;
     fireRate = true;
     shootTime = 0;
 
 }
 
 function Update () {
 
     hudAmmo.text = "Loaded:" +ammo;
     hudMags.text = "Stored:" +mags;
 
     if(Input.GetButton("Fire1")){
         if(ammo > 0){
             if(shootTime > 0.1) {
                 ammo--;
                 audio.clip = shootSound;
                 audio.Play();
                 Instantiate(bullet,gunBarrel.position,gunBarrel.rotation);
                 shootTime = 0;
                 if(aimingDownSights){
                     animation.Play("ADSAnim3");
                     } else {
                     animation.Play("FireAnim1");
                 }
             }
         }
     }
         
     if(ammo == 0 && !audio.isPlaying){
         audio.clip = emptySound;
         audio.Play();    
         
     }
         
     shootTime += Time.deltaTime;
     
     if(Input.GetKeyDown(KeyCode.R)){
         if(ammo == 0 && mags > 0) {
             audio.clip = reloadSound;
             audio.Play();
             animation.Play("ReloadAnim");
             ammo = ammo + 45;
             mags = mags -45;
         }
     }
 
     if(Input.GetButtonDown("Fire2")){
         aimingDownSights = true;
         animation.Play("ADSAnim1");
     
     }
     
     if(Input.GetButtonUp("Fire2")){
         aimingDownSights = false;
         animation.Play("ADSAnim2");
     }    
 }
 

Well i'm thinking about crating another script like this, but changing the ammo and etc...Also i'll make another Bullet-FirePoint(Position where the bullet is instantiated) and etc...

So what's the easiest way to do this? I was thinking about something like: Inside Main Camera I'll create another empty called SecondaryWep, then i'll attatch the Gun2 script, and put inside another gun model, and the other Bullet-FirePoint...then use "activeSelf" to enable and/or disable the guns when i press 1 or 2...Can someone help me with this script? Also if you know a better way to do this please tell me (and please write the script, if you can)...

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Dec 11, 2014 at 09:34 PM

Programming is programming so either language is fine. C# is generally faster than JS but JS is still fine. I see you use java yourself, which I'm not familiar with but here's a java tutorial on YouTube.

Weapon Switching

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

26 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

Related Questions

unlocking weapon (fps) 1 Answer

Multiple Weapon Animation Help 0 Answers

FPS Camera+weapon setup? 0 Answers

Main Camera moving comparively to player weapon. 0 Answers

How do I add a weapon/object to my character? 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