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 - apocalypse now?

Enough prattle. It's bomb time!

  1. Create a new JavaScript and name it Bomb. Drop it in the Scripts folder if you're keeping things tidy.
  2. Open up the script and type the following code:
    function Update()
    {
    transform.position.y -= 50 * Time.deltaTime;
    if(transform.position.y < 0)
    {
    transform.position.y = 50;
    transform.position.x = Random.Range(0,60);
    transform.position.z = -16;
    }
    }
    

We've seen these keywords before, so there should be no surprises here. On every update cycle, move the bomb down in the Y-axis by 50 units per second:

transform.position.y -= 50 * Time.deltaTime;

If the bomb has fallen through the floor,

if(transform.position.y < 0) {

pop the bomb back up to the top of the apartment building:

transform.position.y ...

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