Demo_cs (move, switch guns, shoot bullets)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.