While the “bouncing and sliding” scenes we created were cool, they stop just short of being actual games because there’s no interaction between the user and the scene. Setting up the objects and watching the beach ball succumb to gravity can certainly be placed in the “having fun!” category, but until there are rules and a goal, it’s not quite a game.
To interact with the scene we need to be able to make something happen when we press a key on the keyboard or click on a game object in the scene. This week we’re going to explore using the mouse by creating a script that can be attached to game objects. That script will include a function called OnMouseDown that will be called whenever the mouse is clicked on that object.
Another Unity API call we’ll be using allows us to get a random number between a certain range. What can we use that for? How about to create enemies at random locations in the scene? You’ll find many uses for random numbers in game development.
We’ll also focus on part of the API that gives us the power to populate our scene with as many prefabs as we want — on the fly! Since we have already seen how to Destroy a game object, it’s time we looked at creation.
And as part of the code that uses Instantiate we’ll also take a look at InvokeRepeating(), which allows us to execute a certain chunk of code every 3 seconds, or whatever time period we choose. It’s a really handy bit of code!
You’ll know the answers after these lessons!
Back to the Beginning 2D Game Development Dashboard