O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 Game Object 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 Game Object 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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required