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.
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.
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); } }
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(); } } }
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.
3.131.152.166