unity官方案例--坦克大戰(zhàn)(五)

在名為Model的文件夾中找到Shell模型,將其拖入Hierarchy面板中,怎加Capsule Collider組件到Shell對象當中,將Capsule Collider的Is Trigger屬性勾選上,設(shè)置Direction屬性為z-Axis,Center改為(0,0,0.2),Radius改為0.15,Height為0.55。

圖5.1 ?Capsule Collider屬性


圖5.2 ?子彈膠囊體

增加一個剛體(Rigidbody)組件到Shell對象當中,在Prefabs文件夾中找到ShellExplosion預(yù)制體,將其拖入到Shell對象,并對ShellExplosion對象的AudioSource組件的添加聲音,文件名為ShellExplosion,將Play On Awake的勾勾去除。重新選擇Shell對象,增加一個Light組件。

下面,我們給Shell對象新建一個名為ShellException的腳本,腳本主要實現(xiàn)以下幾個功能

1.攻擊

2.受攻擊坦克受到傷害,

3受攻擊坦克受到?jīng)_擊波的影響

4.聲音和粒子效果

5.整理剩下的游戲?qū)ο?/p>

下面我們逐個進行實現(xiàn)

首先定義一個計算傷害的函數(shù)

private float CalculateDamage(Vector3 targetPosition) {

Vector3 explosionToTarget = targetPosition - transform.position;//構(gòu)建一個三維坐標,存儲目標到炸彈的向量

float explosionDistance = explosionToTarget.magnitude;//計算爆炸點到目標的距離

float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;//計算傷害比例

float damage = relativeDistance * m_MaxDamage;

damage = Mathf.Max(0f, damage);//傷害值最小為0;

return damage;

}

調(diào)用OnTriggerEnter函數(shù),官方文檔對這個文檔的解釋是:This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger.

Notes: Trigger events are only sent if one of the colliders also has a rigidbody attached. Trigger events will be sent to disabled

MonoBehaviours, to allow enabling Behaviours in response to collisions.(自己翻譯:這些信息發(fā)送給勾選了Is trigger的碰撞體或者是剛體,然后觸碰到標簽。注意,標簽事件只有當其中一個碰撞體為剛體時才會有反應(yīng)。)


private void OnTriggerEnter(Collider other) { ? ? ?

Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);//返回指定爆炸半徑范圍內(nèi)的碰撞體 ? ?

? ?for (int i = 0; i < colliders.Length; i++) { ? ? ? ? ?

? ? ? ? Rigidbody targetRigidbody = colliders[i].GetComponent();//找到目標的rigidbody ? ? ? ? ? ? ? ? ? ? ? ? ? ?if(!targetRigidbody){ ? ? ? ? ? ? ? ?continue; ? ? ? ?} ? ? ? ? ? ?//增加沖擊波作用力 ? ? ? ? ? ?targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius); ? ? ? ? ? ?TankHealth targetHealth = targetRigidbody.GetComponent();//找到TankHeath腳本,將其關(guān)聯(lián)到rigidbody當中。

if (!targetHealth)

continue;

float damage = CalculateDamage(targetRigidbody.position);

targetHealth.TakeDamage(damage);//將這個傷害作用于坦克

}

m_ExplosionParticles.transform.parent = null;//不解除子彈的粒子系統(tǒng)???

m_ExplosionParticles.Play();//粒子系統(tǒng)發(fā)揮作用

m_ExplosionAudio.Play();//爆炸發(fā)聲;

ParticleSystem.MainModule mainModule = m_ExplosionParticles.main;

Destroy(m_ExplosionParticles.gameObject, mainModule.duration);

Destroy(gameObject);//消除子彈


public void start(){

Destrop(gameObject,m_MaxLifeTime);

}

完成ShellException腳本后,將Shell的子對象ShellException拖入到ShellException腳本的Explosion Particles和Exception Audio當中。將公共參數(shù)Tank Mask改為Player。 將Shell對象拖入到Prefabs文件夾當匯總,然后保存為一個Prefabs。


圖5.3 ?ShellException參數(shù)設(shè)置

保存場景,將Shell從hierarchy面板中刪除。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容