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

Time for action — moving the cube

To add more movement to the cube, perform the following steps:

  1. Press the Ctrl + Up arrow key to work with the maximized area of the Logic Editor panel. We will be using three more keyboard sensors, let's start creating them.
  2. We have a D or Right arrow key connected. Let's do the rest. Add a new sensor called Keyboard again and press the Left arrow key in the blank cell as shown in the following screenshot:
    Time for action — moving the cube
  3. Add a new Keyboard sensor again and press the Up arrow key as shown in the following screenshot:
    Time for action — moving the cube
  4. For the last one of the keyboard sensors, press the Down arrow key as shown in the following screenshot:
    Time for action — moving the cube

    As seen in the previous screenshots, sensors have been closed by clicking on the triangle on the left-hand side of the header to get more space. But we realize that Keyboard1, Keyboard2, and so on do not mean much to us. I recommend renaming Sensors in order to make them accessible and readable in an easy way when we have more sensors in the list.

  5. Just click on the field that displays, for example, Keyboard3, and rename it to Key Down. Use this process to rename the rest of the Sensors:
    Time for action — moving the cube
  6. Add all of the Keyboard sensors to a controller each, as we did for the first one, by clicking on Add Controller, and select And as the controller and connect them each, one to one:
    Time for action — moving the cube
  7. Add three more Actuators by clicking on Add Actuator and selecting Motion.

    Change the value in each cell as shown in the next screenshot. In this case, the left key is assigned -0.20 in x axis and the up/down keys are used for the rotation of our cube, the value of the up key is 0.20 in the Rot Z axis and -0.20 for the down key in Rot Z.

    Time for action — moving the cube

    Note

    Of course, we need to rename all our active Actuators, similar to how we had renamed our Sensors (as shown in Step 5). Spend a little time renaming them to, for example: Move Right, Move Left; Rotate Right and Rotate Left.

    At the end of this exercise, our cube should move while we press the key correctly. But wait! The last step is to connect all of the bricks together.

  8. Drag-and-drop to connect all of them (as shown in the following screenshot) and just press the Ctrl + Up arrow key (minimize the Logic Editor area) and finally press the Start button:
    Time for action — moving the cube

    Tip

    If you wish to maximize the 3D View area, press the Ctrl + Up arrow key after having clicked on the 3D View area with the Left mouse button as shown in the following screenshot:

    Time for action — moving the cube
  9. For starting the Game Engine, press P. You will be able to move your cube around as shown in the following screenshot:
    Time for action — moving the cube

    We can move our cube around the screen. That's fantastic!

What just happened?

We managed to easily and intuitively work with Logic Editor. We created an interaction with our character by adding the following:

  • Sensors to recognize the keyboard
  • Basic controllers to link the actuators to the sensors
  • Actuators that will move the cube, by following the inputs from the keyboard added as sensors

As you have seen, many operations with logic bricks are repeated. But in the end, we change a numeric value of the actuator, which facilitates the learning by repetition so that it's not difficult to understand how the other bricks work.

We learned how to move our cube in the 3D scene by connecting logic bricks. It is as easy as giving orders by using the keyboard. You could increase the mobility options if you have more buttons to press.

Pop quiz — exploring the interface of the Logic Editor

  1. If you want to create a jump motion, which local axis must you change?
    1. x
    2. y
    3. z
  2. Which option do you choose if you want to go in the opposite direction of the x axis while rolling?
    1. Subtract the X value
    2. Add a value less than X
    3. Change the Y value
  3. Which cell modifies the values of rotation?
    1. Loc X, Y, Z
    2. Loc in X or Y or Z
    3. Rot X, Y, Z
    4. Rot in X or Y or Z

Certainly, you do not need the answers. It is so easy to watch the result instantaneously that it is much better for you to prove all your answers and change if the result does not satisfy you.

Have a go hero— doing more

You know how easy it is to change the values of our cube. If the rotation is slower than you like, change it! And find the best result for you.

If the screenplay is empty and you need another object for reference, just add other cubes in the same scene. Select Add | Mesh and choose Cube, scale it by pressing S and move it around in your scene. Repeat this twice and try to move your cube characters without touching them. This is a good practice to see how you play games with the keyboard.

Note

Point of active view

We did not discuss the view that we used in this chapter. In the games, we normally have various cameras. We will learn how to change this in Chapter 5, Gameplay. For the moment, the active camera is the active view in our 3D View. For better understanding of how to move the cube, the top view is the active view of this game exercise.