Game Engine - Weapons
Blender 3d tutorial
NOTE: In this tutorial, just like nearly all of my tutorials, I have provided what I call keystrokes lines. These are highlighted throughout the tutorial and are meant to allow you to see the actual keystrokes that I went through in order to get the results I get in the tutorial. More advanced users should be able to go through a tutorial without the keystrokes lines assuming I have explained myself sufficiently.
Creating a first person shooter is a very popular topic for those new and old to the Blender Game Engine. In many games, such as in the Halo series, a person can walk up to a weapon on the ground and pick up the weapon by pressing a button. Through this tutorial, you will learn a way to simulate picking up a weapon. It is not the way, nor will it be best for a big game. But if you are trying something simple, this should work for you.
To start, I have created a file for you to download. Basically it has our scene set up and I also have movement applied to the camera, while will allow us to move when we play our game. A few other things you should note are that I have parented the gun right beneath the camera to the camera. There is also a second camera in out scene that is not parented to anything.
The first thing we are going to do is animate the picking up of the gun. As you can already tell, we are actually going to have two guns; one laying on the ground and a hidden one that will not appear (to the user) until the other one is picked up. I am just going to do a basic movement upwards and downwards to hide/show the gun. To start animating, change the screen layout to Animation. You can find the dropdown to do this directly to the right of the help button.
Change Layout to Animation
Make sure that you have selected the gun that we want to animate, the one that is parented to the gun. The other gun will just disappear when we pick it up so no animation is necessary. To set the initial position of the gun, which is when we cannot see it through the camera, just press the I key and selecting Location in the popup. After that, we need to go to frame 25, with the gun still selected, and move it along the Z axis by 0.5. This is the final position of the gun. Insert another keyframe by pressing the I key and choosing Location again. After that, all of our animating is complete.
Select the gun, Press I-->Location, Go to frame 25, G, Z, 0.5, Press I-->Location
At this point we can return to the Game Logic layout. The next thing we need to do is choose a key that will be used to pick up a gun we find on the ground. Personally, I am going to use the Enter/Return key, but you can choose any key you like. To go about setting this up, select the gun parented to the camera. Now add a Keyboard Sensor, an And Controller, and a F-Curve Actuator and connect them all together.
Add a Keyboard Sensor, an And Controller, and a F-Curve Actuator, connect the columns
The only thing that we need to change for the Keyboard Sensor is the Key value, so you can change that to the key you want to use to pick up the weapon. Over in the F-Curve actuator, we want to change the End setting to 25 and in the dropdown menu currently set to Play should be changed to Ping Pong. Changing this to Ping Pong will allow us to hide our weapon after we pick it up.
Set the Key setting for the Keyboard sensor to the Enter Key, Change Play to Ping Pong and change the End setting to 25 in the F-Curve actuator
Now that the animation is all set up, we now need to make it so that the gun that is lying on the ground disappears. Otherwise it wouldn't look like we actually picked up that gun. Select the gun that is lying on the ground and in the Game Logics window add a Keyboard sensor, an And controller, and a Visibility actuator. As usually, connect the logic bricks together to each other.
Select the gun on the ground, Add a Keyboard sensor, an And controller, and a Visibility actuator, connect each column together
Before we move on, there are two settings we need to change. The first and probably most obvious is the Key setting for the Keyboard sensor. This setting should be the key you used to start the gun animation that we made earlier. If you choose a different key then they won't activate at the same time, which isn't what we are going after. The second setting we want change is just to turn of the Visibility checkbox in the Visibility actuator. Having it on would make the object visible, having it off hides it.
Make the key setting the enter key, Turn off Visible setting
If you test your game out now, you should be able to see the camera gun rise up when you hit enter and the gun on the ground to disappear at the same time. However, this isn't exactly what we want to do. We only want to be able to pick up the gun on the ground when we are near it. If this was a real game, we could be on the opposite end of the level or map and still be able to pick it up.
Select the gun on the ground and add a Near sensor. This time we will not need to add a controller or actuator because we are just attaching the near sensor to the controller we already have. You will also have to add a Near sensor and connect it to the existing controller for the gun parented to the camera.
If you have wondered what the use of the controller has been, this is where it comes into play nicely. Since it is an And controller, it requires that all of the sensors that connect to it are activated before it will allow the actuator to work. So in this case, we need the Enter key pressed And we need to be near the gun for the visibility or the animation to actually work. If we change the controller to the Or setting, then you would need the Enter key Or to be near the gun.
Select a gun, Add a Near sensor, Connect the Near sensor to the controller, repeat for the other gun
But hang on. If you test it out you will find that it didn't work. As it is now, neither gun has any idea what they have to be near for that sensor to be activated. So, if it is near any object, it is activated. To solve this problem we have to give both the gun on the ground and the camera gun a property. With the camera gun selected, click on Add Game Property in the left panel of the Game Logic window. Rename that new property Pickup. We also want to change the Property setting in the Near sensor to Pickup2. Then, for the gun on the ground, give it a property name of Pickup2. In the property field of the Near sensor type in Pickup.
Select the camera gun, Click Add Game Property, Change property name to Pickup, change the Property setting for the Near sensor to Pickup2
Select the gun on the ground, Click Add Game Property, Change property name to Pickup2, change the Property setting for the Near sensor to Pickup
At this point, you should be able to test your game out and see it in action. The animation and the gun on the ground should only work when we are close to the gun on the ground and when we hit the enter key. Now you know one way to pick up a gun, or any object for that matter, in the Blender Game Engine.