Unity Artificial Intelligence Programming
上QQ阅读APP看书,第一时间看更新

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, 1.5f); 
    }