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