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.
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: