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"))
{
}
}
}
}
Your answer
Follow this Question
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