Time for action – Readying the weapon

For this example we will create a new project called gun range so that we can easily test our gameplay without having to build and deploy the entire project. Whenever you are making a large play mechanic change to your game, it is often useful to do this in isolation so that you can be more agile in your development. Large projects can get cluttered pretty quickly so it's best to test in simple projects:

  1. Create a new project called GunRange. Save the scene in the project as level_0.
  2. Import a static gun model and put it in a hierarchy group called Weapons.
  3. Next we need to ensure that the weapon is pointed along the Z-axis. We accomplish this by rotating the model so that the business end (that is, the end our projectiles will come out of) is pointed along the positive Z-axis.

    Time for action – Readying the weapon

As we will attach this entire weapon and its gunfire particle system to the character as a prefab, we want to make sure that we define everything at this point relative to the coordinate system of the prefab. This is so that when it is attached to our character, or other object, it behaves properly.

What just happened?

We just imported a gun model to our scene and aligned it to the Z-axis. We did this relative to the prefabs, coordinate system and not specifically to the game's coordinate system. We did this because we want projectiles being fired along the prefabs Z-axis regardless of what else is going on in the world's coordinate system.

It doesn't really matter which axis you align the weapon to (so long as you adjust your model and scripts), but it makes logical sense to align the weapon so that it is pointed down the Z-axis so that when we fire projectiles they are going down the positive Z. Why? In the traditional orientation for 3d graphics, movement along the positive Z-axis represents things moving away from the camera – so it makes sense to use that here.

Firing projectiles

We have two approaches that we can use to add a particle system to our weapon. We can either add it directly to our gun's Game Object or we can add a child Game Object to the gun that does the actual emitting. The approach you choose really depends on how much flexibility you need versus your desire to have more objects in the hierarchy.

If you add the particle system directly to the gun's Game Object, the particles will be emitted from the center of the Game Object and that would not be visually correct. The more universal case is to create a Game Object that will serve as the emission point and attach that to a point on the weapon. This is the approach we will use for our project.

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

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