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