Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
-1
Question by Trevor 1 · Apr 01, 2010 at 11:53 PM · fpsweapons

Player Weapon Scripting

I want to have the player start off with the standard 2 weapon deal. Well thats done. But also find other weapons onn the way. How would i go about this when I go to edit the PlayerWeapons script from the tutorial. this is what I got so far it doesnt work though. If thier is a better script please show me what it is, ive been botherd for 5 days now about it.

function Start () {

 // Select the first weapon
 SelectWeapon(0);

}

function Update () {

 // Did the user press fire?
 if (Input.GetButton ("Fire1"))
     BroadcastMessage("Fire");

 if (Input.GetKeyDown("1")) {
     SelectWeapon(0);
 }   
 else if (Input.GetKeyDown("2")) {
     SelectWeapon(1);
 }   
 else if (Input.GetKeyDown("3")) {
     if (gameObject("AssultRifle").SetActiveRecursively(true));{
     SelectWeapon(2);
     }
     else if (gameObject("AssultRifle").SetActiveRecursively(false));{
     SelectWeapon(null);
     }

}

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 The-BOOM · Apr 02, 2010 at 12:00 AM 0
Share

D: Could you please make the code a little more readable?

avatar image Trevor 1 · Apr 02, 2010 at 12:35 AM 0
Share

I dont know what you want me to do with it thats how most of it was made. selecting a weapon is just plane and simple to begin with.

avatar image qJake · Jun 08, 2010 at 09:53 PM 0
Share

He was talking about the code formatting on the site. You need to highlight the code and click the "Code Format" button if you're going to post code here. I fixed this post for you, but in the future don't forget this.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by xToxicInferno · Jun 08, 2010 at 10:09 PM

Well i do not know if this is the 'best' way but you can do it like i did it.

var weapon: int = 0;

function Update () { if (Input.GetKeyDown("1") { weapon = 0; } if (Input.GetKeyDown("2") { weapon = 1; }

 if (Input.GetButton ("Fire1")){
     if(weapon == 0) {
     //pistol stuff
     }
     if (weapon ==  1) {
     //assault rifle stuff
     }
 }

}

You can also have 'weapon' be static so when you pick up a new gun it changes it to 3 so you can do:

    if (weapon ==  3) {
    //rocket launcher stuff
    }

If you want to do a Metal Gear Solid thing where once you get a weapon you unlock it forever (i guess its similar in other games its just i like MGS haha) you can do:

var rocketlauncher : boolean = false;

if(rocketlauncher) { if(Input.GetKeyDown("3") { weapon = 3 } }

You can then also have rocketlauncher be static so when you get a rocketlauncher it changes it to true.

Well good luck!

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Swapping Weapons In Game 0 Answers

Camera Recoil,Please help me. 2 Answers

C# How to have weapon pickup 1 Answer

Unity3D HELP! How can i pickup/hold/throw object when im rolling ball? 4 Answers


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