- Home /
How to pickup and then Change/Switch Weapons
Hi, I'm curious about how Companies Implement weapons in their games, Do they Disable and Enable scripts and Models to change weapons, or do they have a technique which makes the weapon follow the player that picks it up.
@Dryden Richardson, I redirected your other question here. They were asking the same thing, just worded differently so I tried to answer both at least somewhat.
Answer by Peter G · Jul 16, 2011 at 06:34 PM
There are a couple techniques based on how the gun pickup system works.
As far as picking up guns is concerned, often the gun is parented to the player.
function OnPlayerPickUpWeapon (playerID : float , gunPosition : Vector3 ) { gunTransform.parent = playerTransform; gunTransform.position = new Vector3(GunPosition); } Obviously this isn't working code, its just to get the idea across. The player sends a message to the gun that they want to pick it up. The gun responds by parenting itself to the player. Then it is positions itself properly in the players grip based on the information given to it.
When it comes to swapping between a primary and secondary weapon, the guns are usually just enabled and disabled when needed.
function SwitchWeapons () { primaryWeaponGO.SetActiveRecursively(false); //turn the primary weapon and all its children off. secondaryWeaponGO.SetActiveRecursively(true); //turn the secondary weapon on. } In this setup, the weapons would overlap if both were active at the same time. We keep both guns positioned in firing position and make one invisible while the other is active. This way we don't have to deal with moving them around.
Dastardly Banana has done work with weapon scripts and I don't know how they liscense their code, but I would check it out.
Other threads to check out.
Equiping Weapon to a Character
FPS Switching Weapons on iPhone
A different techinique that actually destroys your old weapon and instantiates the new one:
Thanks for the help, I had a look at Dastardly Banana before but i got confused, thanks for the other threads though!
Your answer
Follow this Question
Related Questions
Weapon Switching 1 Answer
Switching Weapons (C#) 2 Answers
Problem with my weapon change system 0 Answers
Weapon Switching 1 Answer
Change Weapon by its Script int 0 Answers