Here, we're going to be looking a bit more in depth at what the MoleScript does and how it works.
In Unity, you can have many scripts and attach them to different game objects so each object has it's own behaviour, and in our game the MoleScript controls the mole objects.
The behaviour we want from them is fairly simple: the mole comes out of the hole, waits to be hit, and goes back in the hole. There's a little bit more to it than that, but essentially that is what we want. You can see the complete version of the script here, but for discussion purposes we will break it down into separate parts.
This is the variable that holds the mole sprite itself. The sprite is of type tk2dClippedSprite and we make it public so we can easily drag and drop the mole sprite on to the script in the Unity interface. We could make it private and get the mole object using code, but why make things difficult on ourselves?
This holds the height of the mole image. We need to use this to help us to calculate where the mole is to be drawn on screen.
The speed at which the mole travels up and down.
I've added this variable so we can specify how long the mole waits to be hit. The value can be adjusted to make things easier or more difficult.
An invisible rectangle surrounds our sprite enclosing it completely, essentially giving us the mininum and maximum X and Y co-ordinates for our sprite. We store this information in the spriteRec variable.
Holds whether or not the mole has been whacked on its current animation.
The height (Y position) at which we will be drawing the mole sprite on the screen.