這一篇文章,我們將學(xué)習(xí)坦克如何射擊
選擇Hierarchy的Tank對(duì)象,右鍵選擇Tank對(duì)象然后選擇Created Empty。重命名為FireTransform。設(shè)置Position為(0,1.7,1.35),Rotation為(350,0,0)。

右鍵選擇Hierarchy面板的Canvas,并選擇UI下的Slider,將Slider重命名為AimSlider。按住Alt鍵選擇AimSlider旁邊的箭頭進(jìn)行展開(kāi),刪除Background和Handle Slide Area對(duì)象。重新選擇AimSlider對(duì)象找到Slider組件。

不勾選Interactable,設(shè)置Transition為None,設(shè)置Direction為Buttom To Top。設(shè)置Min Value為15,Max Value為30。如圖6.2所示。
同時(shí)選中AimSlider和其子對(duì)象FillArea。
展開(kāi)Fill Area然后選擇Fill,在Rect Transform中,設(shè)置Hight為0,將Fill對(duì)象的Image組件使用小圓鈕選擇名為Aim Arrow的圖片資源,使用Rect Tool(快捷鍵為T(mén))對(duì)邊框的左右進(jìn)行拖拉,使他和坦克模型一樣寬,將AmiSlider的Rect Transform的left、Top、PosZ、Right、Bottom設(shè)置為(1,-9,-1,1,3)。
下面我們給坦克新建一個(gè)腳本,命名為shooting。腳本實(shí)現(xiàn)的功能有
1.檢測(cè)開(kāi)火按鈕的狀態(tài);
2.更新發(fā)射命令;
3.當(dāng)發(fā)射按鈕釋放或者達(dá)到最大充電時(shí),生成炮彈。
下面實(shí)現(xiàn)代碼如下:
public int m_PlayerNumber = 1;//玩家數(shù)量
public Rigidbody? m_Shell;//炮彈實(shí)體
public Transform m_FireTranform;//發(fā)射口
public AudioSource m_ShootingAudio;//發(fā)射時(shí)的聲音
public AudioClip m_FireClip;//飛行過(guò)程的聲音
public float m_MinLaunchForce = 15f;//最小的發(fā)射力
public float m_MaxLaunchForce = 30f;//最大發(fā)射力
public Slider m_AimSlider;//箭頭層
public AudioClip m_ChargingClip;
public float m_MaxChargeTime=0.75f;
private bool m_Fired;
private string m_FireButton;
private float m_CurrentLaunchForce;
private float m_ChargeSpeed;
private void OnEnable() {
Debug.Log("啟動(dòng)");
//當(dāng)坦克啟動(dòng)時(shí),重設(shè)發(fā)射力和UI。
m_CurrentLaunchForce = m_MinLaunchForce;
m_AimSlider.value = m_MinLaunchForce;
}
// Use this for initialization
void Start () {
m_FireButton = "Fire" + m_PlayerNumber;
m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) /m_MaxChargeTime;//發(fā)射力,越久發(fā)射的越遠(yuǎn)
}
// Update is called once per frame
void Update () {
m_AimSlider.value = m_MinLaunchForce;
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 void Fire() {
m_Fired = true;//每次開(kāi)火只能調(diào)用一次;
Rigidbody shellInstance=Instantiate(m_Shell, m_FireTranform.position, m_FireTranform.rotation) as Rigidbody ; //初始化炮彈,并存儲(chǔ)到rigidbody中。
shellInstance.velocity = m_CurrentLaunchForce * m_FireTranform.forward;//設(shè)置炮彈發(fā)射速度。
m_ShootingAudio.clip = m_FireClip;
m_ShootingAudio.Play();//改變發(fā)射時(shí)的聲音。
m_CurrentLaunchForce = m_MinLaunchForce;//設(shè)置發(fā)射時(shí)的力。
}
然后找到Shell預(yù)制體,將其拖入到Shell參數(shù)方框中,找到FIreTransform,將其拖入到FireTransform參數(shù)中,找到Canvas子對(duì)象并將AimSlider拖入到AimSlider參數(shù)中,找到坦克的第二個(gè)Audio Source(沒(méi)有音樂(lè)的clip,Loop和Play On Awake屬性也沒(méi)有勾選的),將其拖入到Shooting Audio參數(shù)當(dāng)中。用小圓鈕將Charging Clip的值設(shè)為FireClip,同樣的方法,將Fire Clip 設(shè)置為ShotFiring。選擇Tank模型,并點(diǎn)擊上面的應(yīng)用。