Unity3d notes-tanks Tutorial Knowledge points Summary---How to fire bullets

Source: Internet
Author: User

Firing bullets requires

1\ Launch coordinates-----Transform firetransform

2\ Bullet-----Gameobject Shell

3\ Relative size Force-----float Maxforce

4\ Maximum launch time----float Maxchargingtime

5\ storage tank-------Slider Aimslider

6\ Related Sounds-----audiosource

Aimslider does not need to drag the bar and does not need a background

You can set Fillarea and fill to make the graphic effect.

PO Code

usingUnityengine;usingUnityengine.ui; Public classtankshooting:monobehaviour{ Public intM_playernumber =1;  PublicRigidbody M_shell;  PublicTransform M_firetransform;  PublicSlider M_aimslider;  PublicAudiosource M_shootingaudio;  PublicAudioClip M_chargingclip;  PublicAudioClip M_fireclip;  Public floatM_minlaunchforce =15f;  Public floatM_maxlaunchforce =30f;  Public floatM_maxchargetime =0.75f; Private stringM_firebutton; Private floatM_currentlaunchforce; Private floatM_chargespeed; Private BOOLm_fired; Private voidonenable () {M_currentlaunchforce=M_minlaunchforce; M_aimslider.value=M_minlaunchforce; }    Private voidStart () {M_firebutton="Kindle"+M_playernumber; M_chargespeed= (M_maxlaunchforce-m_minlaunchforce)/M_maxchargetime; }        Private voidUpdate () {//Track the current state of the "Fire button" and make decisions based in the current launch force.M_aimslider.value =M_currentlaunchforce; if(M_currentlaunchforce >= m_maxlaunchforce &&!m_fired) {M_currentlaunchforce=M_maxlaunchforce;        Fire (); } Else if(Input.getbuttondown (M_firebutton)) {m_fired=false; M_currentlaunchforce=M_minlaunchforce; M_shootingaudio.clip=M_chargingclip;        M_shootingaudio.play (); } Else if(Input.getbutton (M_firebutton) &&!m_fired) {M_currentlaunchforce+ = M_chargespeed *Time.deltatime; M_aimslider.value=M_currentlaunchforce; } Else if(Input.getbuttonup (M_firebutton) &&!m_fired)        {Fire (); }    }    Private voidFire () {//Set the fired flag so only fire is called once.m_fired =true; //Create An instance of the shell and store a reference to it ' s rigidbody.Rigidbody shellinstance =Instantiate (M_shell, M_firetransform.position, m_firetransform.rotation) asRigidbody; //Set the shell's velocity to the launch force in the position ' s forward direction.shellinstance.velocity = M_currentlaunchforce *M_firetransform.forward;; //Change the clip to the firing clip and play it.M_shootingaudio.clip =M_fireclip;        M_shootingaudio.play (); //Reset the launch force. This was a precaution in case of missing button events.M_currentlaunchforce =M_minlaunchforce; }}

Unity3d notes-tanks Tutorial Knowledge points Summary---How to fire bullets

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.