In the first installment of Megabite, we covered some of the framework and terminology that makes up Unity3. These, of course, are simply the boring pieces of framework that make up the foundation of getting yourself started. (Be sure to start there if you are planning to follow along)
We’ll pick up where we left off: a cube and a camera. When played, the game merely consists of the arrow keys being scripted to manipulate your cube on the most basic of levels. Next, we’re going to accomplish something fun - we’re going to make our cube fire bullets that are capable of destroying targets.
First Things First:
In order to make sure we are all on the same page here, we want to position our camera so that the controls make the most sense. Based on the script we had before, we want to place our camera above the cube and rotate it using the inspector. What I have (pictured below) is an X rotation of 90 degrees and a y-rotation of 90 degrees as well. When played, this gives us a view that makes the cube go upward when the up arrow is pressed, and down when the down arrow is pressed.
The positioning and rotation can be altered as you see fit, but the following tutorial will fire a GameObject from the right side of the cube that will move in a negative direction along the Z-axis.
Crafting Your Bullet
This is our universe, so we make the rules. We don’t have to make things aerodynamic, and things don’t have to make sense according to our physical world. If you want your bullet to be the size of a truck, that will be entirely possible. We will be attributing our own physics and making everything from scratch, including the laws of physics.
To begin, use the “Create” button on the hierarchy to make a shape. You can choose a cylinder, capsule, cube, or sphere. The scale of the object will be much more important than the actual shape of it. Change the Transform Position of the new object at 0,0,0 and scale it to something that makes sense for you. After that, use the blue Z-axis arrow to move your new object to the right side of your cube. Because your project might vary slightly from my own, make a note of the z-position of your object when you have moved it far enough away from the cube so that it no longer touches.
At this point, we’re going to add something new to the bullet – a Rigidbody. Rigidbodies are GameObjects that our custom physics apply to. For our purposes, our bullet be able to accept a variable amount of force and fly across the screen at a rate that we choose. To do this, select Component -> Physics -> Rigidbody from the main menu.
Our world still does not have a “ground” to fall toward, so we want to turn off gravity for the Rigidbody component via the inspector (as pictured). All else can remain the same.
Pre-Fabulous:
Remember when I mentioned Prefabs in Megabite #1? Well, now this is where they come in handy. Obviously we’re going to want more than a single bullet. We want the ability to make as many as we want and we also desire for those projectiles to fly across the screen.
Create your very first Prefab by selecting Assets -> Create -> Prefab from the main menu, and you should see a new object appear within your assets. Name this object “Bullet”. Once that is done, just click and drag your bullet shape from the hierarchy tab and drop it on top of your brand new Prefab. What this does is essentially create an asset out of a GameObject, and it also gives us a single thing to manipulate that will change every instance of “bullet” we create in the game.
Believe it or not, your next step is to delete your bullet shape from the scene. After all, we don’t want to start the game with a bullet just sitting there, do we? Right-click your shape in your hierarchy and delete it. On the next page, we will script the firing mechanism and get that bad boy flying.

Hello , I have got a problem. i typed everything exatly like you shoved here, but I get the error :
“”
Script error: OnCollisionEnter
This message parameter has to be of type: Collision
The message will be ignored.
“”
my code is like it should be :
“”
var force : float = 120;
function Start(){
rigidbody.AddForce(Vector3.right * force);
Destroy(gameObject, 5);
}
function Update(){
}
function OnCollisionEnter(collision : Collision)
{
Destroy(gameObject);
Destroy(collision.gameObject, 3);
}
“”
Please could you help me ?