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