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 Michael 12 · May 12, 2011 at 08:39 PM · weaponpickup

weapon pickup and equiping it to FPS player

I know this has been asked but I can't find an answer that suits what I'm trying to do.

I have a setup where my FPS player starts off in a house with no weapon selected.

He finds on on the wall and picks it up. I'm using this as my pickup for the weapon:

var gotShotgun : boolean = false;

var gunPickupSound : AudioClip;

function Start (){ //playerWeapons is the players selectable weapons playerWeapons = this.GetComponent("PlayerWeapons"); }

function OnTriggerStay (){

 if (Input.GetButtonDown("Pickup")){
     gotShotgun = true;
     Destroy(gameObject);


 //Play Gun Pickup Sound
 if (gunPickupSound)
 AudioSource.PlayClipAtPoint(gunPickupSound, transform.position);

 print("You Picked Up The Double Barel Shotgun");
 }

}

The gun dissapears from the wall and it prints out that you have "You Picked Up The Double Barel Shotgun"

So far so good.

What I can't figure out is how to get it to activate and load up the shotgun from my PlayerWeapons script

var weapons : GameObject[]; var selectedWeapon : int;

function Awake () { // Select the first weapon SelectWeapon(0); }

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

if (Input.GetKeyDown("1") && weapons.length >= 1) { if(!weapons[0].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(0); selectedWeapon = 0; } } else if (Input.GetKeyDown("2") && weapons.length >= 2) { if(!weapons[1].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(1); selectedWeapon = 1; } } else if (Input.GetKeyDown("3") && weapons.length >= 3) { if(!weapons[2].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(2); selectedWeapon = 2; } } else if (Input.GetKeyDown("4") && weapons.length >= 4) { if(!weapons[3].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(3); selectedWeapon = 3; } } else if (Input.GetKeyDown("5") && weapons.length >= 5) { if(!weapons[4].gameObject.GetComponent("weaponLockout").isLocked){ SelectWeapon(4); selectedWeapon = 4; }

 }

}

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

3 months of trying to learn this code stuff and a lot of it still makes my brain hurt lol. Anyone know how I can make this work?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by flaviusxvii · May 12, 2011 at 08:47 PM

First of all. This code:

 if(!weapons[0].gameObject.GetComponent("weaponLockout").isLocked){
            SelectWeapon(0);
            selectedWeapon = 0;
        }

Is a good candidate for a function:

function selectWeaponByIndex(var i:int):void
{
    if(!weapons[i].gameObject.GetComponent("weaponLockout").isLocked) {
            SelectWeapon(i);
            selectedWeapon = i;
    }
}

So when you want weapon "1" you can just call selectWeaponByIndex(1).

Now.. when you pick-up the shotgun you should broadcast a message that you did. Have your PlayerWeapons script respond to this by selecting the shotgun.

Comment
Add comment · Show 7 · 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 Michael 12 · May 12, 2011 at 09:04 PM 0
Share

Thanks flaviusxvii, not sure I really get what you are doing here, can you perhaps explain in more detail for those of us code challenged ;)

avatar image flaviusxvii · May 12, 2011 at 09:14 PM 0
Share

I don't know what you're asking me to clarify? Functions? Broadcasting? You already have examples of both in your code. Use those as templates.

avatar image Michael 12 · May 12, 2011 at 09:20 PM 0
Share

O$$anonymous$$ well, like I say, this code stuff a lot I don't get so when I changed my code to what you have here I get an error message that I don't know what to do about:

Assets/WeaponScripts/PlayerWeapons.js(15,10): BCE0044: expecting (, found 'selectWeaponByIndex'.

avatar image flaviusxvii · May 12, 2011 at 09:42 PM 0
Share

http://pastebin.com/idbGRjG$$anonymous$$ <-- Like this for the function change..

avatar image Michael 12 · May 12, 2011 at 10:40 PM 0
Share

Ok I changed my script to that but it does not equip my gun to my player, so what am I missing? It took me a while just to see what was different about my script and the one you have.

Show more comments
avatar image
0

Answer by Kashaunzilla · May 17, 2011 at 03:56 AM

Try this.

var shotGun = Transform; private var shotGun : machineGun;

function OnTriggerEnter () { Destroy (gameObject); if ( machineGun ) { machineGun.enabled = true; } }

All you have to do now is to make the mesh activate. More detail needed, let me know.

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

weapon pickup 1 Answer

How can I detect if box is touching a tag named weapon? 1 Answer

How do I fix my gun animation 0 Answers

PickUp Weapon in Brick Breaker Game 1 Answer

ramdomised weapon pickup 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