x


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.

more ▼

asked Jul 16 '11 at 05:42 PM

Dryden Richardson gravatar image

Dryden Richardson
135 37 45 47

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

Jul 16 '11 at 06:38 PM Peter G
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

There are a couple techniques based on how the gun pickup system works.

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

Multiple Melee Weapons

more ▼

answered Jul 16 '11 at 06:34 PM

Peter G gravatar image

Peter G
15k 16 44 136

Thanks for the help, I had a look at Dastardly Banana before but i got confused, thanks for the other threads though!

Jul 16 '11 at 07:11 PM Dryden Richardson
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x380
x237
x222
x11
x3

asked: Jul 16 '11 at 05:42 PM

Seen: 3714 times

Last Updated: Jul 16 '11 at 07:11 PM