Time for action – apocalypse now?
Enough prattle. It's bomb time!
Create a new JavaScript and name it
Bomb
. Drop it in the Scripts folder if you're keeping things tidy.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 = 50;
and use Random.Range
to make it appear in a different spot along the top of the building. This will create the illusion that there are multiple bombs falling from the sky.
Save this script. Just as we did with the...