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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Maakat · May 09, 2012 at 02:22 AM · javaweapons

Picking up and Dropping Weapons

The base of my game is from the FPS tutorial, and so were many parts of this script. Its a game were theres a console on screen, and after you unlock a weapon, you can type the weapons name into the console to equip it. But I dont want to be able to get it through the console unless you found it.

 //These are what you collide with to get permision to get weapon from console
 
 var MG : Transform;
 var RL : Transform;
 var PG : Transform;
 
 //These functions were supposed to allow acces to the weapon you unlocked when they were called... Mega fail
 var MGselect = function() {
 if (Weapon Console == "MG") {
 SelectWeapon(1)
 }
 }
 var RLselect = function() {
 if (WeaponConsole == "RL") {
 SelectWeapon(2)
 }
 }
 var PGselect = function() {
 if (WeaponConsole == "PG") {
 SelectWeapon(3)
 }
 var WeaponConsole : String = "Weapon Choice";
 
 function OnGUI () {
     //The console you type into to accses the weapon you got
     WeaponConsole = GUI.TextField (Rect (10, 10, 200, 20), Weapon Console, 25);
 }
 
 function Start () {
     //Default Weapon
     SelectWeapon(0);
     
 }
 
 function Update () {
     // Did the user press fire?
     if (Input.GetButton ("Fire3"))
         BroadcastMessage("Fire");
     
     
     if ( Vector3.Distance(MG.position, transform.position ) < 2) {
         SelectWeapon(1);
         MGselect();
         //calling the functions
     }    
     if ( Vector3.Distance(RL.position, transform.position ) < 2) {
         SelectWeapon(2);
         RLselect();
     }    
     else if ( Vector3.Distance(PG.position, transform.position ) < 2) {
         SelectWeapon(3);
         PGselect()
     }    
 }
 
 function SelectWeapon (index : int) {
     for (var i=0;i < transform.childCount;i++)    {
         // Activate the selected weapon
         if (i == index)
             transform.GetChild(i).gameObject.SetActiveRecursively(true);
         // Deactivate all other weapons
         else
             transform.GetChild(i).gameObject.SetActiveRecursively(false);
     }
 }
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 Piflik · May 09, 2012 at 10:21 AM

In my game I have all available weapons in an array in a script attached to the player. To switch weapons I just have to increase/decrease the variable that determines which element of the array should be used. A second variable determines how high this first variable can go. You can always check the player's input against the second variable and if he wants a 'higher' weapon than the variable, the game will not give him the weapon. But this obviously only works when you know exactly in which order the player will find the weapons.

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

Multiple Cars not working 1 Answer

Variable not assigned, but is assigned. Help! 1 Answer

SCRIPT NOT WORKING 2 Answers

To save your game and load it. 1 Answer

"Empty name" error ??? 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