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 stasgar · Sep 11, 2016 at 08:05 PM · arraysinventory systemupdate functiongetaxis

Inventory system, GetAxis actions? (complex problem)

Hello. i made an inventory system for my game - up to 4 items in hands, one is active. Player can drop/use item, by pressing 1 button the red and yellow item change each other (and so on).

But the code is terrible, as a programmer i feel pain looking at it. So the principle is: Inventory empty object attached to the MainCamera, and 4 position objects attached to the Inventory Object (pic 1).

I use 2 GameObject arrays to store Positions and objects, attached to them (pic 2).

alt text

I use RayCast to check if an object is Pickupable(c# script named "Pickupable exists) and then perform my PickUp action. So the first problem i have is using GetAxis in Update. I used a trick, in simple form it looks like this:

         if(Input.GetAxisRaw("Fire1") !=0 && PickUpOn)
         {
             // Code of picking up here...
             PickUpOn = false;
         }
 
         if(Input.GetAxisRaw("Fire1") == 0)
         {
             PickUpOn = true;
         }

Is there a way to perform this in a better way?

The next problem is in Changing items by pressing 1,2,3 buttons. I made this:

     void ChangeItem()
     {
         
         if(Input.GetAxisRaw("First Item") !=0 && FirstItemOn)
         {
             if(InventoryItem[3]!=null)
             ChangePosition(3,0);
             FirstItemOn = false;
         }
 
         if(Input.GetAxisRaw("First Item") == 0)
         {
             FirstItemOn = true;
         }
 
         if(Input.GetAxisRaw("Second Item") !=0 && SecondItemOn)
         {
             if(InventoryItem[2]!=null)
             ChangePosition(2,0);
             SecondItemOn = false;
         }
 
         if(Input.GetAxisRaw("Second Item") == 0)
         {
             SecondItemOn = true;
         }
 
         if(Input.GetAxisRaw("Third Item") !=0 && ThirdItemOn)
         {
             if(InventoryItem[1]!=null)
             ChangePosition(1,0);
             ThirdItemOn = false;
         }
 
         if(Input.GetAxisRaw("Third Item") == 0)
         {
             ThirdItemOn = true;
         }
     }

So GetAxis problem + logical uglyness. Any ideas? ))

And also my ChangePosition method:

     void ChangePosition(int first, int second)//first/second - index in InventoryItem array
     {
         temp = InventoryItem[first];
         InventoryItem[first] = InventoryItem[second];
         InventoryItem[second] = temp;
 
         InventoryItem[first].transform.parent = InventoryPosition[first].transform;
         InventoryItem[first].transform.localPosition = Vector3.zero;
         InventoryItem[second].transform.parent = InventoryPosition[second].transform;
         InventoryItem[second].transform.localPosition = Vector3.zero;
     }


inventory-system.jpg (71.5 kB)
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

67 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

Related Questions

Weird Z position behavior 0 Answers

Need help looping through an array. 0 Answers

What should I use to store item stats? List, Arrays or something else? 0 Answers

how to use 3d objects for inventory slots 2 Answers

Enemys stop following after destroying gameobject 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