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

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.