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 /
  • Help Room /
avatar image
0
Question by ThePolygators · Feb 14, 2020 at 12:47 AM · scripting problempickupweapon systemweaponchanging

I need help picking up and dropping weapons

Hello my name is Ilias and first off, im about to go insane..

Now that we have this out of the way to my problem. Im working since 2 days on creating a solid "simple" weapon system. So at first simply changing weapons was not a problem. I just linked 3 weapons (that are parented to my player) to my keys 1, 2 and 3 and activated or deactivated them on KeyDown. I wanted to go further and make these weapons dropable and pickupable if this is a word. Wich wasnt the biggest problem either. But when it came to having multiple weapons ( and beeing able to expand to more weapons if i want to) the real problem ocurred. I think my biggest problem is identfiying wich weapon is being picked up. I tryed using tags but it became really messy and being forced to add new lines of code everytime i want to add a weapon wasnt really what i wanted. About my weapon system that im trying to achieve:

I want to use only 3 buttons for weaponselection. 1 for pistols 2 for rifles and 3 for special weapons. And it would be awesome if i can get it to work that i just have to place all the weapons on the player and just having to SetActive them instead of physicly transforming the position each time into players hand. But i have no idea how to make that if i pickup a weapon, it becomes the active pistol or rifle etc. I can activate SOME pistol or SOME rifle but i have trouble making specifically the one i "have" at the moment active. I tryed a array and working with integers of "currentPistol" and activating certain ones in the list but again, dont know how to identify wich one i have. So i scraped all that and tried to make some kind of "physical" inventory. 3 empty gameobjects parented to player. Rifle Holster Pistol Holster and Special Holster. so i tought when i press 1, pistolHolster gets transformed to hand. And if i pickup a weapon, the picked up weapon with tag of what weapon it is simply gets parented to rifleHolster or pistolHolster etc. like that i dont have to worry about names and stuff i just specify wich weapons get transformed to wich holster. But i dont really know how todo that either.

Im just kinda lost and idk what is the best and simplest way to achieve the system id like to have so id be verry happy if someone can give me some advice or lead me in the right direction. Oh and im working with 3 booleans btw. pistolBool = active etc. and now im even struggeling defining if pistolBool = active, the other 2 get deactivated and the other way around. i can deactivate on other boolean but idk how to do multiple ones without writing 100 if statements.

here is what i have so far:

 public Transform camera;
     public Transform player;
 
     public Transform pistol;
     public Transform rifle;
     public Transform eyePos;
     public Transform pistolHolster;
     public Transform rifleHolster;
 
     public bool pistolBool = false;
     public bool rifleBool = false;
 
     public float pickupRayDist = 10.0f;
 
     private void Start()
     {
 
     }
 
     private void Update()
     {
         WeaponSelection();
 
         PickupWeapon();
     }
 
     void WeaponSelection()
     {
 
         // pistol selection
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             pistolBool = !pistolBool;
         }
 
         if (pistolBool == true)
         {
             pistol.transform.parent = eyePos.transform;
             pistol.position = eyePos.transform.position;
             pistol.eulerAngles = eyePos.eulerAngles;
         }
         else if(pistolBool == false)
         {
             pistol.transform.parent = pistolHolster.transform;
             pistol.position = pistolHolster.position;
             pistol.eulerAngles = pistolHolster.eulerAngles;
         }
 
         // rifle selection
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             rifleBool = !rifleBool;
         }
 
         if (rifleBool == true)
         {
             rifle.transform.parent = eyePos.transform;
             rifle.position = eyePos.transform.position;
             rifle.eulerAngles = eyePos.eulerAngles;
         }
         else if (rifleBool == false)
         {
             rifle.transform.parent = rifleHolster.transform;
             rifle.position = rifleHolster.position;
             rifle.eulerAngles = rifleHolster.eulerAngles;
         }
 
         // deactivate eachother
 
         switch(bool)
     }
 
     void PickupWeapon()
     {
         Ray pickupRay = new Ray(camera.transform.position, camera.transform.forward);
         RaycastHit hit;
 
         Debug.DrawLine(camera.transform.position, camera.transform.position + camera.transform.forward * pickupRayDist, Color.red);
 
         if(Input.GetKeyDown(KeyCode.F))
         {
             if (Physics.Raycast(pickupRay, out hit, pickupRayDist))
             {
                 if (hit.collider.CompareTag("Pistol2"))
                 {
                      
                 }
             }
         }
     }
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

0 Replies

· Add your reply
  • Sort: 

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

288 People are following this question.

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

Related Questions

Storing diffrent bullet amounts for different Weapons. 1 Answer

My script doesnt work? 0 Answers

Weapon Select Menu 0 Answers

Health pickup script issue -1 Answers

Pickup weapons in 2D scroller game 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