Blender Game Engine:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action — exploring the logic bricks world

All objects have two options, one of which is a label with their name (in this case Cube), and the other a button labeled Add Sensor. The Add Sensor button adds a new sensor to the object. The following steps will help you in exploring the world of logic bricks:

  1. Select the Cube with RMB. Click on Add Sensor and select Keyboard as the sensor in the sub-menu listed:
    Time for action — exploring the logic bricks world
  2. Be sure to select the cube with RMB to operate with logic bricks. Press the D key or Right arrow key in the blank cell with no label if you want to move the object, for example, to the right. The key that you press will be assigned to the sensor. This is the active key, which will trigger the positive pulse. Click inside the button again if you want to change or un-assign the key:
    Time for action — exploring the logic bricks world
  3. Next, click on Add Controller. The first controller in the menu is called And. This is the default type when you create a new controller. It can be used to simply pass a Sensor event directly to an Actuator (as shown in the next point):
    Time for action — exploring the logic bricks world
  4. Next, click on Add Actuators, and choose Motion:
    Time for action — exploring the logic bricks world
  5. Now, change the value of x axis to 0.20 in Loc (Location). The three grey boxes in each row is the motion on the x, y, and z axes (in that order). If you want a diagonal motion, add values to all of the axes at the same time. If you want to rotate the cube, enter 0.20 in Rot (Rotation) of x axis:
    Time for action — exploring the logic bricks world

    The cube jumps the number of blender units that we input, this can cause objects to go through and around our scene.

    Tip

    The L button switches whether the motion will be added to the local or global axis. Local axis is the object's own axis. This is the most common option and is the default. With the global axis, the cube movement can be aligned with the axis of the world.

  6. We will now connect this blocks system together. Click-and-drag from the socket (small circle) at the end of Sensor to the socket at the start of Controller:
    Time for action — exploring the logic bricks world
  7. Then click-and-drag from the socket at the end of Controller to the socket at the start of Actuator:
    Time for action — exploring the logic bricks world

    In this example, the sensor is responsible for moving the cube forward if we push the Right arrow key.

  8. Press the Start button to play the game in the Properties Scene panel:
    Time for action — exploring the logic bricks world

    The cube will not move automatically. Press the Right arrow key, and it will start to move forwards. When you stop pressing the key, the cube will stop moving. Keep the key pressed to move the cube further. Press Esc to return to Logic Editor of Blender.

    If you're happy with what you've seen, click on the Start button again, you can do it as often as you like.

    Tip

    Change the size of the view in the desired panel by moving the cursor near the limits of the header menus, moving horizontally and then vertically.

    You can also press the Ctrl + Up arrow key to get a completely maximized view (be sure you click on the 3D View panel before doing so).

What just happened?

Great! You just accomplished the three parts of logic bricks, and there is a connection among them. Let's review how to do that and we will be ready to understand the parts!

  • Sensor

    The Keyboard sensor is for detecting keyboard input. The first blank cell is for single key presses. We pressed the D Key, or the Right arrow key, to move in the x axis of our cube. Sensors are grouped by objects, with every selected object appearing in a list and under each object are its sensors. It is possible to filter them if we check/uncheck which sensors are viewed. We can select which sensors to be viewed from the following options:

    • Sel: Shows all of the selected objects' sensors
    • Act: Shows only the active objects' sensors
    • Link: Shows the sensors which have a link to a controller
    • State: Shows only those sensors connected to a controller of the current state

    But for the moment, let us leave them as they are.

  • Controller

    The controllers are the bricks that collect data sent by the sensors.

  • Actuator

    The actuators initiate their functions when they get a positive pulse from one (or more) of their controllers. They set an object into motion, for example, rotation.

    Simple Motion applies motions in our cube without frictions. If we need a character with resistance, we need to choose Servo Control, which consists of a servo controller that adjusts the force on the object in order to achieve a given speed, and hence the name Servo Control. For the moment, Simple Motion is right.

Of course, the cube has got only one interactive key, and will go ahead if we push the Right arrow key in the keyboard. However, this just introduces us to the fundamentals of the Blender Game Engine. You just need to try, test, and modify it. It is easy, isn't it?

Hey! We are finally programming video games! In some aspects, this is similar to being a computer programmer.

By adding more logic, we are ready to continue making a game. But first, let us try to finish an entire movement of our cube so that you can move freely in our private 3D scene.