درس: برميل متفجر
برميل ينفجر حين يُطلق عليه النار أو يُصطدم به بقوة. يُدفع كل ما حوله ويتأذى وتهتز الكاميرا.
1. الإعداد

اصنع Cylinder وأعطه جسمًا صلبًا وcollider وضع السكربت عليه.
2. السكربت

Scripts/Explosive.cs
public class Explosive : Behaviour
{
public float radius = 6f;
public float push = 12f;
public float damage = 60f;
bool gone;
public void TakeDamage(object amount) { Explode(); }
public override void OnCollisionEnter(GameObject other)
{
// something fast ran into it
if (other.rigidbody.exists && other.rigidbody.velocity.Length > 8f) Explode();
}
void Explode()
{
if (gone) return;
gone = true;
foreach (GameObject near in Physics.OverlapSphere(transform.position, radius, gameObject))
{
// nearer things are pushed harder and hurt more
Vector3 away = near.transform.position - transform.position;
float strength = 1f - Mathf.Clamp01(away.Length / radius);
if (near.rigidbody.exists) near.rigidbody.AddImpulse((away.Normalized + Vector3.up * 0.5f) * push * strength);
near.SendMessage("TakeDamage", damage * strength);
}
Camera.Shake(0.6f, 0.5f);
Audio.PlayAt("Audio/explosion.wav", transform.position, 1f, 1f, 60f);
GameObject.Destroy(gameObject);
}
}
احفظه باسم Scripts/Explosive.cs.
3. كيف يعمل

- تستدعي الرصاصة
barrel.SendMessage("TakeDamage", 50f)؛ فتفجّره دالةTakeDamageفي البرميل. انظر الصحة لهذه الرسالة. - الاصطدام السريع يفجّره أيضًا: تفحص
OnCollisionEnterسرعة الكائن الآخر. - تسرد
Physics.OverlapSphereكل ما داخلradius. ولكل شيء يتناقص الدفع والضرر مع المسافة: الأقرب يُصاب أكثر. - تهز
Camera.Shakeالمنظر؛ وتشغّلAudio.PlayAtصوتًا في موضع البرميل (أضفAudio/explosion.wav).
4. تفاعلات متسلسلة

ضع برميلين متجاورين: انفجار الأول يستدعي TakeDamage على الثاني فينفجر هو أيضًا.
التالي: موجات من الصناديق.