Having a hard time wrapping my head around picking up fps guns script.
Hello there internet people, Im fairly new to scripting/unity. Ive been slowly but surely working on an fps shooter, Ive got allot of things working including my own animations, guns ive modeled in blender etc etc. Here is my question, Im a complete dunce with scripting.
my set up consists of Maincamera -gunlocation --primary ---shotgun --secondary ---pistol
I made a script that sets active either the primary or secondary empty game object that has the guns as child objects so effectively it switches from primary to secondary and back.
I would like to add more guns to the primary and secondary so if lets say I walk up to the gun, press F it switches the shotgun off and turns on an smg or the like. I understand the aiming at the weapon on the ground with raycasts in a limited range from the player part.
I cant wrap my head around actually switching the weapon from the shotgun to the smg.
If anyone has some insight into how they would go about doing something like this it would be much appreciated. Maybe some helpful funcions that would be applicable.
int weaponSelected = 1;
public GameObject primary, secondary;
// Start is called before the first frame update
void Start()
{
SwapWeapon(1);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
if (weaponSelected != 1)
{
SwapWeapon(1);
}
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
if (weaponSelected != 2)
{
SwapWeapon(2);
}
}
}
void SwapWeapon(int weaponType)
{
if (weaponType == 1)
{
primary.SetActive(true);
secondary.SetActive(false);
weaponSelected = 1;
}
if (weaponType == 2)
{
primary.SetActive(false);
secondary.SetActive(true);
weaponSelected = 2;
}
}
}
Your answer
Follow this Question
Related Questions
How to restrict rotation? 0 Answers
How to get bullet hole to wrap around corners of objects? 0 Answers
Raycast over Photon 0 Answers
Compensate slow fall when using rigidbody.drag 0 Answers
Gun Distortion 0 Answers