Unity 3D

Prefabs

[1.] Setup your scene with a main camera, a directional light, a plane, a cube and a sphere.

Rename the main camera to Player, the plane to Floor, the cube to Wall and the sphere to Bullet.

[2.] Drag your bullet into the project window - Note that the icon changes and the text colour changes. It has become a prefab.

A Prefab is a GameObject complete with all its components, property values, and child GameObjects as a reusable Asset. 

The Prefab Asset acts as a template from which you can create new Prefab instances in the Scene. It essentially allows you to create many clones of a GameObject.. 

You can now delete the Bullet from the Hierarchy. We will spawn bullets when the Player clicks the left mouse button. To do this we need to write custom code. Create a new C# Script and call it Player.

Player Script




Line 7: Creates a reference to the bullet prefab.



Line 12: Calls the shoot method continually.


Line 17: Checks if the left mouse button is down


Line 19: Create a prefab

Line 20: Create a reference to this bullet

Line 22 & 23: Create random x and y values

Line 24: Create a new Vector3 to set the shooting direction (bullet angle)

Line 25: Normalize the shooting direction.


Vectors

Vectors are a fundamental mathematical concept which allow you to describe a direction and magnitude. In games and apps, vectors are often used to describe some of the fundamental properties such as the position of a character, the speed something is moving, or the distance between two objects.

We normalize a vector to separate direction and magnitude usually so that magnitude can be scaled by multiplying it by another value.

Bullet Script

Lines 5 - 8: Create and expose variables to the inspector.

Line 13: Add a physical force to the Rigidbody component of the  bullet to propel it in the direction we choose and at the speed we want. (Vector)

Line 19: Every frame check if there are bullet clones which we should destroy.

Line 24: If the bullet collides with something which has the tag "ExplosionTrigger" then create the confetti.

Line 26: Create a reference to the explosion gameobject prefab.

Line 27: Set the position of the confetti to the position where the bullet collided with the wall.


destroyBullets method works by subtracting time from timeToLive variable.

When the variable reaches 0 the bullet is destroyed.




Create 3 new GameObjects, I have used cylinders but it looks much better with cubes. Call the object ExplosionParticle1, ExplosionParticle2, ExplosionParticle3 . Give them each a material and pick different bright colours. Add a Rigidbody component to all of them. Then drag them to the project window to make them all into Prefabs. Now delete them from your scene.

Create a new C# and call it ExplosionParticles. Attach the script to each Prefab.

ExplosionParticles Script

Line 7 - 8: Expose variables which we can adjust in the inspector.



Lines 11 - 15: Choose random values for the direction of the particles and assign them to a variable.

Line 16: Create a random value within a max range for the explosion force.

Line 17: Add that force to the Rigidbody of the particle.


Update method: Destroy particles after a second.

Explosion Trigger

Create an Empty GameObject and call it Explosion. Drag it into the project window to make it a Prefab. Now delete it from the scene. Create a new C# Script and call it Explosion. Attach the script to the prefab.

Explosion Script

Lines 7: Create an array (List) of particle game objects.

Line 8: Create a variable which will set the number of particles

Line 12: Initialise a for loop which will create the particles.

Line 14: Create a random number of particles.

Line 15: Position the particles at the position of the Explosion GameObject.