blenderNetwork patreon blender-logos d heart user th book photo pencil map-marker chevron-right comment comments twitter facebook rss chain navicon google-plus envelope linkedin calendar-o tumblr reddit stumbleupon share-alt blogger flattr flag

Game Engine - Shooting

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.

Just like my other game engine tutorial I have a premade scene already to go. I do this because I want to focus of the topic of this tutorial, the actual game engine, and not walk you through how to model and set up a scene. I am sure you already know how to do that already, so there is no reason to force you through it. That said, you can download that file now if you like. Of course, it's not absolutely necessary if you are just getting an idea as to how I did this.

You can see what my scene looks like in the image below. It is pretty simple. I have a wall of cubes that have already been set to be dynamic. They are on a plane and there is a cylinder acting as our gun and a sphere which is going to be our ammunition. I have also set up the movement of the camera and our gun. This is extremely similar to the motion tutorial. All I did was set up the arrow keys to rotate the camera and gun up, down, left, and right. You should be able to test it out when you open the file. Lastly there is an empty at the front of the gun that I will explain in just a bit.


To start we want to make it so that a new cannonball is going to appear when we hit our shoot key. So start by selecting the cannon. Down in the game logic window, add a Keyboard sensor, an And controller, and an Edit Object actuator. After you do that, connect the logic bricks together. Now in the Keyboard sensor change the Key setting to the key you want to use to shoot our cannonball. I am just going to use the space bar.

Add a Keyboard sensor, an And controller, and an Edit Object actuator, Connect them together, set the Key setting to the Spacebar

Now we need to tell the Edit Object actuator what object it is supposed to add to the scene. All you have to do is change the Object setting to the name of the cannonball. If you are using the same file I am, it should be named Sphere. Then change the Time setting to 50. This is how long out cannonball will exist in our scene. I believe it translates to the number of frames the object will last, so this would be two seconds. After the time runs out it disappears again. Next we want to change the Z value for the Linear Velocity setting to -50 and to click on the L button to the right of that. This makes the coordinates local so that the cannonball will shoot in the direction that the cannon is pointing in.

Change Object setting to Sphere, Time to 50, the Z linear velocity to -50.00, and turn on the L button


Before we test out the game we have a few things to do. Select our cannonball and go to the Physics settings. You should be able to see a Physics Type setting near the top of the window. We need to change this setting to dynamic, otherwise the cannonball will never move. Back in the 3D view window, with the cannonball still selected press M and choose any layer other than the first one. The reason we need to do this is that the Edit Object actuator that is connected to the cannon will not work unless the object is on a non-visible layer in the game. So, we just move the cannonball to a different layer and it should work.

Select the sphere, go to the Physics settings, change Physics Type to Dynamic, Press M in the 3D view window, and select a different layer.


Now we have all the logic brick and all of the setting for the cannonball complete. However, if you test the game out now, you will find that either the cannonball will now appear when you hit your shoot key or (if you do not look through the camera) that the cannonball falls behind the cannon. The reason for this is that the cannon's Physics Type is set to Static. This allows it to effect dynamic objects in the scene. The easy fix is to select the cannon, go in the Physics setting and change the Physics Type to No Collision. This will allow dynamic objects, and therefore the cannonball to go through the cannon and come out of the front.

Select the cannon, go to the Physics settings, change Physics Type to No Collision


Ammo Limiting

Ok, so we have our cannon and it can shoot an unlimited amount of cannonballs. Well, if we are shooting for the realism, then we only have a limited about of ammo. So, what we are going to do now is make it so we only have a certain amount of ammo to shoot.

005.jpgLet's jump right into this. Select the cannon. If you look in the game logic window, there should be a left side panel labeled Properties. Click on the Add Game Property button and a few options should appear. In the first field, change the name of the property to Ammo. Next we are going to change the dropdown setting to Integer. Lastly, in the third field, change the 0 to the amount of ammo you want to have. I suggest doing something small for testing, since we will not be creating an interface to tell you how much is remaining.

Click on Add Game Property, Change the name to Ammo, Change the type to Integer, and change the value to 5.

The next thing that we need to do is add two more logic bricks for the cannon. The first is a Property sensor and the second is a Property actuator. Connect the sensor to the one controller we have and connect the one controller to our new Property actuator. Let's start changing the settings in the Property sensor. What this sensor does, if you are not familiar with programming, is tests whether or not the value of the property it is watching equal, not-equal, etc. to some value you choose to test the property against. What we need to do is make sure that the value of our ammo does not equal zero. With that in mind, change the Evaluation Type setting to Not Equal, change the Property setting to the name of the property we are checking, which is Ammo, and lastly change the Value setting to 0.

Add a Property sensor and Property actuator, connect the new sensor and actuator to the controller, change Evaluation Type to Not Equal, change Property to Ammo, change Value to 0.


The last settings that we need to change are all in the Property actuator. Again, we are going to be changing all three of the main settings. This time change the Mode setting to Add. We also want to change the Property value to Ammo. So, if you are not a programmer who already understands what is going on, here is what we are doing. The Mode setting is telling the game engine what we want to actually do with the Ammo value. In this case, every time we shoot one cannonball, we want to subtract one from the Ammo value. With the limited settings, we must use the Add setting. Therefore, we are going to change the Value setting to -1. So, if we started with a value of 5 for our Ammo property, then after one shot, it would change to 4, then the next shot to 3, and so on until it hit 0. At that point, the sensor will not be activated and you can no longer shoot the cannon.

Change the Mode setting to Add, change the Property value to Ammo, and change the Value setting to -1


If you haven't already, go ahead and go into camera view and test it out. Hopefully you will find that you can only shoot as much ammo as you set. If you ever decide you want to go back to unlimited you can always go back to the property settings and change Integer to String or something that will never change to zero and change the value to something other than zero. You can also just set the value of the property to some super high number. That's all!

Popular Now


  •  irascibleone / 2543 / AAA
    That's a good question, because I can't find it. Looks like one of the last few crusty old tuts I have needed to update, so it might have gotten lost during a cleanup or site update. Sorry!
  •  stardavid / 113 / rookie+
    Hello, nice tutorial. Where can i download the file?