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 Blink · Aug 08, 2012 at 10:49 PM · gunpickupswap

Gun swap through pickup?

This is hard to explain but here we go. I have 2 guns parented to the main camera, the main camera is parented to an empty gameobject that holds the cameras animations. These 2 guns are on 2 separate fps controllers, I have then made a prefab of both of these plus one that doesn't have any gun at all just the cameras animations.

With this is there any way to swap guns with one that is on the ground that when the player walks over swaps them round? This is the easiest way I can describe what I'm trying to achieve, anyone who can help I would be very grateful.

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 Radon · Aug 08, 2012 at 11:02 PM

I have had the same question, and thankfully, @AldoNaletto had the answer for it.

http://answers.unity3d.com/questions/294476/weapon-pickup-and-change.html

Comment
Add comment · Show 1 · 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 Blink · Aug 09, 2012 at 09:31 AM 0
Share

I'm not the best of scripters and I'm struggling to make sense of of it, could you tell me how it works and such?

avatar image
0

Answer by Radon · Aug 10, 2012 at 01:47 AM

Use this code by @AldoNaletto, First you need to have 2 weapons, primary and secondary. Tag the primary one "MachineGun" and the secondary "RocketLauncher". After applying the code on your controller, put a check mark next to MachineGun. Now, put your secondary weapon on the floor as a gameobject and tag it "RocketLauncher". Create any collider and re-size it to be big. Check the "Is trigger". Then play your project, walk over your weapon on the floor, press "f", and the magic should happen hopefully. Don't forget to set your primary and secondary variables as your weapons in the inspector.

 var hasRocketLauncher = false; // tells if you have a rocket launcher
 var hasMachineGun = false; // tells if you have a machine gun
 var ammoClips = 1; // tells how many ammo clips you have
 var rockets = 10; // tells how many rockets you have
 var Primary : GameObject;
 var Secondary : GameObject;
 private var inTrigger = false;
 private var object: Transform;
 
 function OnTriggerEnter(other: Collider){
   inTrigger = true; // the player entered the trigger
   object = other.transform; // save the object transform
 }
 
 function OnTriggerExit(other: Collider){
   inTrigger = false; // the player left the trigger
 }
 
 function Update(){
   // if player inside trigger and F pressed:
 
   if (inTrigger && Input.GetKeyDown("f")){     
     switch(object.tag){
       case "MachineGun": 
         hasMachineGun = true; // enable switching to the machine gun
         Destroy(object.gameObject); // destroy the picked object
         SelWeapon(1); // select primary weapon automatically
         break;
       case "RocketLauncher":
         hasRocketLauncher = true; // enable switching to the rocket launcher
         Destroy(object.gameObject); // destroy the object
         SelWeapon(2); // select secondary weapon automatically
         break;
       case "AmmoClip":
         ammoClips++; // increment ammo clips
         Destroy(object.gameObject);
         break;
       case "Rocket":
         rockets++; // increment rockets
         Destroy(object.gameObject);
         break;
     }        
   }
   // Code to switch weapons when 1 or 2 is pressed:
   // - key 1 selects primary weapon:
   if (Input.GetKeyDown("1")) SelWeapon(1);
   // - key 2 selects secondary weapon:
   if (Input.GetKeyDown("2")) SelWeapon(2);
 }
 
 // function used to select weapon: activates only the selected weapon and 
 // deactivates all others; only activates a weapon if the player already has it
 
 function SelWeapon(nWeapon){
   Primary.SetActiveRecursively(nWeapon == 1 && hasMachineGun);
   Secondary.SetActiveRecursively(nWeapon == 2 && hasRocketLauncher);
 }
Comment
Add comment · Show 1 · 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 Blink · Aug 10, 2012 at 10:11 AM 0
Share

I'm only having one gun to be held at a time so how could it be changed so you only have a primary weapon? Plus having a primary and secondary it sort of glitches out.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Gun swap through collision 1 Answer

copy settings from other gameobject 2 Answers

Camera Recoil,Please help me. 2 Answers

How to make a pick up gun script? 0 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