Step 10: Making the ducks move up
We'll make the ducks hide behind the crate, and randomly pop up and duck back down again.
Add a script to any duck.
We'll make it so that when this duck is created, it will set its y position to 500 so that it's almost at the bottom of the screen. Grab "When created" from Events, and also grab "set x position of myself" from Transform. Change the x to y, and set it to 500.
Once it is created, we'll make the duck send itself a message to tell it to move upwards. This message will be delayed for a random amount of time so that the ducks don't all pop up at once.
Grab "100 milliseconds have passed" (from Control Flow) and drag it under the "y position" block. Then grab "random integer from 1 to 100" (from Operators) and drag that block into where the "100" block is inside the "100 milliseconds have passed" block. You can drag the "100" block into the trash.
Change the numbers in this block to randomize between 1000 and 3000 (this will choose a time delay between 1 second and 3 seconds).
To send the 'move upwards' message, grab the "send message 'message' to instance other" block, and drag it inside the "milliseconds have passed" block. Replace the "instance other" block with "myself" (from Sensing). Change "message" to "move upwards".
To do something when that message is recieved, grab the "When a message of 'message' is retrieved" (from Events) and drag that block out into the workspace. Change 'message' to 'move upwards'. Then inside this message receiver block, place a "set velocity x" block (from Physics), and change it to "set velocity y to -2000". This will make the duck travel upwards.
Click Level Editor to return to the level (make sure to save this script and name it ducks).
Add the "ducks" script to the other two ducks. First, make sure the ducks layer is selected.
Then open the Scripts sidebar, and click on the "ducks" script to select it.
Now click on the ducks to add the script to them.
Now play the game. The ducks fly up, which is good, but then they keep flying up and disappear! We'll fix that.