The Dead state

If the tank has reached the Dead state, we make it explode:

    protected void UpdateDeadState() 
    { 
      //Show the dead animation with some physics effects 
      if (!bDead) 
      { 
        bDead = true; 
        Explode(); 
      } 
    } 

Here's a small function that will give a nice explosion effect. We just apply an ExplosionForce function to our rigidbody component with some random directions, as shown in the following code:

 protected void Explode() { float rndX = Random.Range(10.0f, 30.0f); float rndZ = Random.Range(10.0f, 30.0f); for (int i = 0; i < 3; i++) { rigidbody.AddExplosionForce(10000.0f, transform.position - new Vector3(rndX, 10.0f, rndZ), 40.0f, 10.0f); rigidbody.velocity = transform.TransformDirection( new Vector3(rndX, 20.0f, rndZ)); } Destroy(gameObject, ...

Get Unity Artificial Intelligence Programming - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.