Your first Blender game, part 2

In the last episode of Your first Blender Game, you learned how to create the playing field, add physics to it and the ball, then connect it to the keyboard for playing, and how to restart it. Now, we’ll

  • add another plane under the playing field so that the ball doesn’t fall out all the way out of Blenderspace, but rather onto a catchplate, and
  • attach a sound to the collision event
  • complicate the game by adding two more keys for rotating the camera around the labyrinth.
The plane! The plane!

The plane! The plane!

This is of course just a proof of concept again, but as you have probably learned how to attach materials to meshes already (if you are interested in the game engine, that is), you can then make your game look just as cool as you want.

But on to this collision detection system. All you need to do is to add a plane below the playing field, then make it static and a collision object. To do that, just add a plane, then go to the Physics tab, and select Static object. Now the plane is aware of its surroundings and will complain if it is hit by another object. Call the plane SoundPlane or something.

Setting the BallHit property

Setting the BallHit property

You also need to add a game property to the ball you are playing with. It doesn’t matter what you call the property, but as it has to do with the ball colliding, I suggest BallHit or something like that. To add it, go to Game Logic, then select  the ball. Properties are to the left of the sensors. Click on Add property, type the name in the field ’prop’, and as its type, select Boolean. As you see, there are many other types of properties too, but here you are interested only in whether a collision takes place or not.

When you now reselect the SoundPlane, you can attach a sensor, controller, and actuator to it. Select Collision as the Sensor type. As the Property, type BallHit. Now the plane will look out for the object with the BallHit property touching it. Add an And controller, then a Sound actuator. Use the Sound actuator’s file selector to find a suitable sound (on Windows machines, there are sound files in the C:\Windows\Media folder), and then set the Play Mode to be Play End. This ensures the sound file is played all the way. Remember to connect the Sensor to the Controller, and the Controller to the Actuator.

Setting the sound to play

Setting the sound to play

When you now run the game, the ball falling through and hitting the SoundPlane will play the sound you set for it.

As a final touch, let’s add a method for rotating the camera around the center of the playing field by pressing A and S. This makes the game fiendishly hard to play. There are many methods to achieve this, but the easiest one is to use a new class of objects, namely an ”Empty” object, and creating a child-parent relationship for it. That relationship means that the objects move together, and if you rotate the Empty, the camera will pan around it, and thus around the center of the playing field.

First, select the labyrinth object, then snap the cursor to it by pressing Shift+S and selecting Snap cursor to selected. Then, press Shift+A and add an Empty. It doesn’t matter what type of Empty you use, I like the plain axes type. It is merely a point in space, it will not do anything and it will not show up in renders or the game engine view, but you can still use it to animate things.

The new Empty object

The new Empty object

When you create a child-parent object relationship, it is important to note the order in which you select the objects. Just as in real life, children come first. So, first select the camera, then press Shift, and select the Empty. Press Ctrl+P and select Object in the Parent to submenu that opens. This creates a dashed line between the Empty and the camera. Now, select the Empty, then add two new sensors for it.

Camera parented to the Empty

Camera parented to the Empty

First create a Keyboard sensor, and press the A key – call it RotCamR. Create an Add controller, then a Motion actuator. This time, you want to rotate the Empty around the Z axis, so make the A key rotation be on the Z axis for 3 degrees. A reciprocal set up for the S key, called RotCamL, with a -3 degree Z axis rotation, will enable you to rotate back. When you now try this, you will quickly see that to succeed now without falling through is rather hard, if you rotate the camera too at the same time you play the game.

The camera rotation logic for the Empty

The camera rotation logic for the Empty

What remains now is to set up targets on the playing field, for example, so that you would try to hit the objects without falling through. You can use the collision detection to play a sound as you hit an object, and use the parenting technique to make the target objects move with the labyrinth plane. There are many ways you could take this game forward, but I’ll leave it up to you to figure out what you want to do next with this rudimentary game.

 

 

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *

*