Time for action – the maaagic of aaaarguments

To make the explosion happen on the thing that hits the enemy ship, rather than on the enemy ship itself, we'll pass the position of the colliding GameObject to the Explode() function.

  1. Open the EnemyShip Script, and make these changes:
  2. In the OnCollisionEnter function, pass the transform.position of the colliding object's GameObject to the Explode() function:
      Explode(col.gameObject.transform.position);
  3. Now make sure that the Explode() function accepts this argument:
      function Explode(pos:Vector3){
  4. And finally, position the instantiated explosion to the pos variable that receives the argument value:
     function Explode(pos:Vector3){ audio.PlayOneShot(audioClips[Random.Range(0,audioClips.length)]); Instantiate(explosion, ...

Get Unity 4.x Game Development by Example Beginner's Guide 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.