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 - give up the func

Let's get that function set up in the EnemyShip Script first. Double-click to open the EnemyShip Script, and get typing.

  1. Write this new function outside of and apart from the other functions in the script:
    function ResetPosition(){
    transform.position.y = 71;
    transform.position.x = Random.Range(-11, 68);
    transform.position.z = 0;
    rigidbody.velocity = Vector3.zero;
    }
    

    These are the very same repositioning lines we already have. You can even just type the shell of the function, and copy/paste those lines right in there. Either way, you need to go back and eradicate those lines from the Update function and replace them with a ResetPosition() function call.

  2. Erase these bolded lines:
    if(transform.position.y < -4) { ...

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