usingUnityengine;usingSystem.Collections; Public classGun:monobehaviour {PrivateAnimator ANI; //Fire Sound PublicAudioClip Fireclip; //loading and changing the sound of bullets PublicAudioClip Reloadclip; //Preparing the Sound PublicAudioClip Readyclip; //Spark Effects PublicGameobject Muzzleflash; //Bullet Preset Body PublicGameobject Bullet; PrivateTransform Firepoint; voidAwake () {ANI= getcomponent<animator> (); Firepoint= transform. Find ("Firepoint"); } Public voidFire () {//If the current animation state is normal if(ANI. Getcurrentanimatorstateinfo (0). Isname ("Normal")) { //Play Fire animationsAni. Settrigger ("Kindle"); //play fire sound in the current locationAudiosource.playclipatpoint (fireclip,transform.position); //Show Spark EffectsMuzzleflash.setactive (true); } } Public voidReload () {//If the current animation state is normal if(ANI. Getcurrentanimatorstateinfo (0). Isname ("Normal")) { //Play AnimationsAni. Settrigger ("Reload"); //Play SoundAudiosource.playclipatpoint (reloadclip,transform.position); } } /// <summary> ///Generate bullets (frame events)/// </summary> Public voidInitbullet () {//Generate BulletsGameobject Currentblt =Instantiate (bullet, Firepoint.position, firepoint.rotation) asGameobject; //add speed to bulletsCurrentblt. Getcomponent<rigidbody>(). Velocity= Firepoint.forward *Ten; }}
usingUnityengine;usingSystem.Collections; Public classGunmanager:monobehaviour {//Current firearm serial number Private intCurrentgunindex =0; //Current Gun Script PrivateGun Currentgun; voidStart () {//Find the default gunCurrentgun =transform. Getchild (Currentgunindex). Getcomponent<Gun> (); } voidUpdate () {if(Input.getmousebuttondown (0)) { //FireCurrentgun.fire (); } if(Input.getkeydown (KEYCODE.R)) {//Change Bullets .currentgun.reload (); } if(Input.getkeydown (KEYCODE.Q)) {//Change the gun .Gunswitch (); } } /// <summary> ///Change the gun ./// </summary> voidGunswitch () {//hide the currently used firearmstransform. Getchild (Currentgunindex). Gameobject.setactive (false); //to replace a gun .currentgunindex++; //prevent sub-object ordinals from crossing//when the serial number equals the number of firearms, the number is zero after taking the remainderCurrentgunindex =currentgunindex%Transform.childcount; //show the new gunstransform. Getchild (Currentgunindex). Gameobject.setactive (true); //Update FirearmsStart (); }}
usingUnityengine;usingSystem.Collections; Public classMuzzleflash:monobehaviour {//Spark Display Time Public floatInterval =0.1f; /// <summary> ///when it was activated,/// </summary> voidonenable () {//after interval time, execute hideInvoke ("Hide", Interval); } /// <summary> ///hide the current game object/// </summary> voidHide () {gameobject.setactive (false); }}
Demo_cs (move, switch guns, shoot bullets)