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 10, 2014 at 02:20 AM · javascripterrorfpsweaponswitching

Weapon Switching

Well i'm making an FPS and i'm having problens with my switch weapons script...well, at the begginning I had onky 1 gun, an M4, then i was thinking about duplicating this Gameobject (and the script, to make different wep stats), so I duplicated it, changed the model and the stats, i now i have 2 guns, but i disabled the fisrt gun (gameobject, in inspector window) to test the second gun, now, i'm receiving a lot of random erros, and when i play the game, i spawn 2 characters, the one that is already in the scene, and one that falls from space (Player(Clone)), I don't know what is causing this and idk the solution...here are all the codes u need to read:

M4 Script:

 #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");
     }    
 }

Desert Eagle Script:

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 = 7;
 mags = 21;
 fireRate = true;
 shootTime = 0;

}

function Update () {

 hudAmmo.text = "Loaded:" +ammo;
 hudMags.text = "Stored:" +mags;

 if(Input.GetButtonDown("Fire1")){
     if(ammo > 0){
         if(shootTime > 0.5) {
             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 +7;
         mags = mags -7;
     }
 }

 if(Input.GetButtonDown("Fire2")){
     aimingDownSights = true;
     animation.Play("ADSAnim1");
 
 }
 
 if(Input.GetButtonUp("Fire2")){
     aimingDownSights = false;
     animation.Play("ADSAnim2");
 }    

}

Ammo Box Script:

 #pragma strict
 //Ammo Box/Recharger script
 
 var distBox : int;
 var rechargeQuant : int;
 var skinAmmo : GUISkin;
 
 function Start () {
 
 rechargeQuant = 225;
 
 }
 
 function Update () {
 
 distBox = Vector3.Distance(GameObject.Find("Player").transform.position, transform.position);
 
     if(Input.GetKeyDown(KeyCode.E) && distBox <= 2){
     
         GunScriptM4.mags = GunScriptM4.mags + 225;
         GunScriptDEagle.mags = GunScriptM4.mags + 21;
         Destroy(gameObject);
     
     }
 
 }
 
 function OnGUI(){
 
 GUI.skin = skinAmmo;
 
     if(distBox <=2){
     
         GUI.Label(Rect(Screen.width /2 -150,Screen.height /2,500,500), "Press E to recharge your gun");
     
     }
 }

PS: All variables are assinged...

Can someone help me??? Please! And i think i'm gonna do the weapon switching script like this:

 //just a reference, i know the name are wrong
 
 var m4 : go;
 var deagle :go;
 
 function start
   m4.activeSelf = true;
   deagle.activeSelf = false;
 
 function update
   if(press 1)
     PickUpM4();
 
   if(press 2)
     PickUpDEagle();
 
 function PickUpM4
     animation.Play("pickupm4");
     m4.activeSelf = true;
     deagle.activeSelf = false;
 
 function PickUpDEagle
     animation.Play("pickupdeagle");
     m4.activeSelf = false;
     deagle.activeSelf = true;
 
 //maybe yields and other commands will be added latter, i did't write this script in my project yet

I apologize about my english, thanks...

EDIT: FORGET ABOUT THE WEAPON SWITCHING THING, I GAVE UP ON THAT FOR NOW, BUT THE GAME STILL SPAWNING 2 PLAYERS WHEN I START THE GAME, CAN SOMEBODY FIX THIS OR KNOW HOW TO?

Comment
Add comment · Show 1
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 FirePlantGames · Dec 10, 2014 at 04:27 AM 0
Share

That should work in theory, but if you're going to add more weapons, I would suggest using a different weapon storage system...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SkaredCreations · Dec 10, 2014 at 10:44 AM

You shouldn't duplicate the weapon script, instead you should create one generic (with the variable that exists for every weapon) and then create the other ones by inheriting from it. Besides that, the "weapon switching" script is full of errors: all the functions in there are missing both the round parenthesis after their names and the braces surrounding their body, also the functions "start" and "update" must be capitalized (letter case is important).

 var m4 : GameObject;
  var deagle : GameObject;
  
  function Start () {
    m4.activeSelf = true;
    deagle.activeSelf = false;
 }
  
  function Update () {
    if (Input.GetKeyDown (KeyCode.Alpha1))
      PickUpM4();
  
    if (Input.GetKeyDown (KeyCode.Alpha2))
      PickUpDEagle();
 }
  
  function PickUpM4 () {
      animation.Play("pickupm4");
      m4.activeSelf = true;
      deagle.activeSelf = false;
 }
  
  function PickUpDEagle () {
      animation.Play("pickupdeagle");
      m4.activeSelf = false;
      deagle.activeSelf = true;
 }

PS: if it's not too late I'd suggest to switch to C# instead of Javascript, much better for readability and probably more supported by Monodevelop with better intellisense.

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 CJGames · Dec 10, 2014 at 09:28 PM 0
Share

I know the WepSwitching script was wrong, i just wrote that way cuz i didn't do the script yet and wrote like that so you could have a preview

avatar image CJGames · Dec 10, 2014 at 10:12 PM 0
Share

Do you know at least why is it spawning 2 players and how can I fix that PLEEASE ANSWER i don't have any backup

avatar image b2przemo · Dec 11, 2014 at 09:07 AM 0
Share

In code above you don't have spawn player function. You must show correct source. That script probably contains association to or other component taget as Player or GameController in inspector.

avatar image
0

Answer by b2przemo · Dec 10, 2014 at 01:00 PM

Already have the same problem. I try solve it by create separate weapon prefab with all scipts like weapon damage, weapon type, weapon modifiers etc. Then attach to player instance all scripts with prefab weapons and set only one of them to active. Switch weapon script simply get current active component disable it and enable component with desired weapon prefab. I've only 3 components of weapon: one meele and two range types. One of them is still active rest are disabled.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Change Automatic to Semi-Automatic 1 Answer

Weapon Switching 2 Answers

Weapon switching. 1 Answer

Problem With Weapon Firing Mechanism 1 Answer

Weapon Switch Animations 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