Time for action – Adding a particle system

  1. Create an empty game object from the GameObject menu and select Create Empty.
    Time for action – Adding a particle system
  2. Name this game object MuzzlePoint.
    Time for action – Adding a particle system
  3. Select the MuzzlePoint game object in the Hierarchy view to bring up the components associated with it.
    Time for action – Adding a particle system
  4. In the Component menu select Ellipsoid Particle Emitter so that it has a particle system associated with it.
    Time for action – Adding a particle system
  5. Position the MuzzlePoint game object on your gun model in Unity to designate where the projectiles should be emitted.
    Time for action – Adding a particle system
  6. The next thing we need to do is have our particle emitter actually emit particles. Since we set up our weapon so that it was pointing down the Z-axis, it is pretty easy to configure our particle system so that it will send the particles down the positive Z-axis as well.

    Remember that since we've made this particle emitter a child of our gun, we want to make sure that the coordinate system we define for it is relative to the origin of the gun model. You will need to adjust it from the gun model's origin to position it properly on the gun, but realize that the coordinate system for the MuzzlePoint is relative to that of the gun.

    Time for action – Adding a particle system

    Since we've made this a child of our gun model, we want to configure it to be at the gun models, origin. You may have to adjust this for your particular model, but you should make sure you start at the gun model's origin.

    Since we are creating a particle system that represents bullets, we want to have all of our projectiles moving along the same axis. To accomplish this with an Ellipsoid Particle Emitter, we change the Ellipsoid for the emitter to only emit particles along one axis, the Z-axis in this case. If we didn't do this, we would have particles that would spray along random axes.

    Time for action – Adding a particle system
  7. Finally, we need to add a simple script, call it Gunfire, to our MuzzlePoint game object that is tied to the fire button of our gun and we're done.
    using UnityEngine;
    using System.Collections;
    
    public class Gunfire : MonoBehaviour {
          
       public GameObject muzzlePoint;
       
       
       // Use this for initialization
       void Start () {
          muzzlePoint = GameObject.Find("MuzzlePoint");	
       }
       
       void fireWeapon() {
    
          
          muzzlePoint.particleEmitter.Emit(1);
          
       }
       
    }
  8. Import the soldier model into our scene as done previously.
    Time for action – Adding a particle system
  9. Adjust the gun in the soldier's hierarchy such that it is a child of the hand so that the soldier is carrying the item and will move in sync with the player as they move.
    Time for action – Adding a particle system
  10. Update the Gunfire script adding a GUI.Button to simulate our fire button in the real game.
    using UnityEngine;
    using System.Collections;
    
    public class Gunfire : MonoBehaviour {
          
       public GameObject muzzlePoint;
       
       
       // Use this for initialization
       void Start () {
          muzzlePoint = GameObject.Find("MuzzlePoint");	
       }
       
       void fireWeapon() {
         
          muzzlePoint.particleEmitter.Emit(1);
          
       }
       
       
       // Update is called once per frame
       void OnGUI () {
          if ( GUI.Button( new Rect(0,0,50,50), "Fire" ) )
          {
             //Debug.Log("Firing the weapon");
             fireWeapon();
          }
       }
    
    }
  11. Run the game and press the fire button.

What just happened?

We just gave our weapon the ability to fire particles that represent bullets. We have also attached that weapon to our normal player so that it is an integral part of our player and will move as the player animates in the scene. We now have one of the fundamental building blocks for a game that involves shooting as a primary play mechanic.

Currently our particles have a default appearance, but if we want to change that appearance we can simply add a Particle Renderer component to this MuzzlePoint game object. We can then change the appearance of the particles by providing our own texture that is representative of what our bullets will look like.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.40.47