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 Joe 3 · Mar 21, 2010 at 12:55 AM · gameobjectweaponpickup

How would go about making a weapon pick up?

Hi I'm making a first person shooter where the player can hold, use and switch between two weapons. I want to be able find a weapon on the ground, pick it up and switch it out with one of my weapons and drop that weapon and use the new weapon. I could really use some help on a semi easy way to do this.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Jason_DB · Mar 21, 2010 at 01:07 PM

What you could first do is create an array for the player's weapons (in a weapon control script similar to the one in the FPS Tutorial). That would allow you to have two easily switched out weapons and leave the option the increase the number of weapons easily. This script would manage the weapons as well as tell them when to fire/reload and would be added to the main camera of your player (the weapons themselves would be children of the main camera). The weapon scripts would have functions called "selectWeapon" and "deselectWeapon" to turn them on or off.

var weapons : GameObject[]; var selectedWeapon : int; function Start () {

// Select the first weapon SelectWeapon(0); }

function Update () { if (Input.GetKeyDown("1") && !(weapons[0].gameObject.GetComponent("weaponlocking").isLocked)) { SelectWeapon(0); selectedWeapon = 0; }
else if (Input.GetKeyDown("2") && !(weapons[1].gameObject.GetComponent("weaponlocking").isLocked)) { SelectWeapon(1); selectedWeapon = 1; }
if (Input.GetButtonDown("Reload")) BroadcastMessage("Reload");

}

function SelectWeapon (index : int) { for (var i : int=0 ;i<weapons.length; i++) { //select the weapon if (i == index){ weapons[i].gameObject.BroadcastMessage("selectWeapon"); // Deactivate all other weapons }else{ weapons[i].gameObject.BroadcastMessage("deselectWeapon"); }

}

What you could then do is create a "selectableWeapon" script, which would only need to have one variable (the weapon that it references). You would put this script on the weapon model in the environment and then tag it as something like "pickupAble" (so that when the player looked at the weapon lying on the ground they can pick it up).

var weapon : GameObject;

You would just need to have every possible weapon be attached to the main camera (all but two of them being deactivated), so that when the player selects a gun off the ground that weapon would activate one of the player's weapons. Here's a "selectweapon" script that you could put on the main camera to actually switch the weapons out.

function Start (){ //playerWeapons is the weapon controller script from above playerWeapons = this.GetComponent("playerweapons"); }

function Update () { //this draws a ray to check if the player is looking at a selectable weapon. It would also be possible to use OnMouseOver unless you have a texture in the middle of your screen (textures block OnMouseOver so if you have a crosshair you can't really use it) ray = transform.camera.ScreenPointToRay (Vector3(Screen.width/2.0,Screen.height/2.0,0)); if(Physics.Raycast(ray,hit,100)) { if(hit.collider.gameObject.tag == "pickupAble") { selectedWeapon = hit.collider.gameObject; } else { selectedWeapon = null;

} if(selectedWeapon && Input.GetButtonDown("pickup")){ for (var i : int=0 ;i<playerWeapons.weapons.length; i++){ if(playerWeapons.weapons[i] == selectedWeapon.GetComponent(SelectableWeapon).weapon) return; }

 playerWeapons.weapons[playerWeapons.selectedWeapon].gameObject.BroadcastMessage("deselectWeapon");
 playerWeapons.weapons[playerWeapons.selectedWeapon] = selectedWeapon.GetComponent(SelectableWeapon).weapon;
 playerWeapons.SelectWeapon(playerWeapons.selectedWeapon);

} }

These are trimmed down scripts from my game, so they would require some tweaking to work in another one. Again, though, a lot of this is derived from the FPS Tutorial if you want to look at that. Also, sorry if this is too long or too specific.

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 Joe 3 · Mar 23, 2010 at 02:17 AM 0
Share

This great help thanks. I was also wondering how to make the weapon on the ground disappear after you picked it up the spawn back later?

avatar image Jason_DB · Mar 23, 2010 at 08:28 PM 0
Share

You could probably deal with that by activating and deactivating the model (by turning off the renderers)

avatar image user-1794 · Apr 19, 2010 at 11:26 PM 0
Share

Be sure to also turn off the pickup collider, not just the shader, so you dont randomly pick stuff up that isnt "there". you could also have it physically spawn your gun prefabs, but I have no idea how to do that.

avatar image bubblegumsoldier · May 09, 2011 at 04:30 PM 1
Share

this doesn't work

avatar image Jason_DB · May 09, 2011 at 09:44 PM 2
Share

The code is just examples pulled from my own system. It won't work as it is, but the design works.

Show more comments
avatar image
0

Answer by GRZN · May 25, 2012 at 06:49 PM

Can u help me with the weapons pack from you? , its says that the Reload input and the pickup input is missing how do i add it? i download your pack

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
avatar image
0

Answer by exvalid · Nov 13, 2017 at 04:32 PM

link text Note i didnt add throwing but that can be done quite easily now its grabable. i was kinda saving that bit to last

Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

Fixed An Inversion Error and a Euler read error while moving. since yesterday it is now working as stated below

ObjectGrabIdAndLock, ObjectReplyIdAndMove

ObjectGrabIdAndLock goes on Main player,.. ObjectReplyIdAndMove goes on Moveable Objects remember to add layers in you want to hit in inspector and pick the Headcam Ect

Updated Scripts Since the other week now with full movement And Rotation and Full Inversion Options

This is Complete bar Diagnals and Mouse Rotation as im adding this now, you want to use the option OverideDiagnals and possibly UseDefaultRotation if u hate my defualt.

too add mouse copy the whole auto inversion and paste it underneath and swap the names to the mouse names instead of the default keys its a mission dont attempt it lol. i will do this over the week as still cleaing up the script its pretty large, Please contact at Exvalid@gmail.com To give me job coding.

cheers Ryan kappeslink text Exvalid@gmail.com link text


objectgrabscripts.zip (11.7 kB)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Swapping Weapons In Game 0 Answers

Making A Universal Pickup Script 1 Answer

Java for android script help??? 0 Answers

Problem with adding number of Potions 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