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.
Open the EnemyShip Script, and make these changes:
In the
OnCollisionEnter
function, pass thetransform.position
of the colliding object's GameObject to theExplode()
function:Explode(col.gameObject.transform.position);
Now make sure that the
Explode()
function accepts this argument:function Explode(pos:Vector3){
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, pos, Quaternion.identity); }
Save the script and try it out. The explosions go off at the site of the collision, and it makes a lot more visual sense.