First Person Shooter

In this tutorial we will:

  • Open .blend file with level assets - barrels and cubes, a brick walls, and a player with camera;

  • Use Logic Nodes to add W S A D movement and a jump with some extras;

  • Upgrade with Logic Nodes into a FPS game;

../../../_images/ln-fps-annotations.png

Placement options for Annotation tool

We will also learn few handy shortcuts on-the-go. Shall we?

Download/open level with assets .blend file. Follow the instructions precisely. :)

  • Shift-Space > D for on-screen scribble tool, called Annotation in blenderland.

Annotation tool is useful for quick sketch, storyboard, and transferring ideas to collaborators. Also for annotations. He he he. Try all three Placement options for Annotation tool, rotate 3D viewport in-between, to see different placements of your scribbles. Go to N-panel > View > Annotations and select individual colors to be able to erase them. Or click X to delete all at once quickly.

Ok, enough chit-chat - let’s fight!

../../../_images/ln-fps-player_hierarchy.png

Player as parent of many children

In Outliner Editor top > search for Player.

Take a look of the level objects, particularly the hierarchy of Player object. Below Player object there is a bunch of other objects, being a children of Player, our main Parent object. Wherever Player goes, all children will follow. Also take a look at Properties Editor - under Object tab (orange square) there are Relations and Collections sub-panels, and close to the bottom, a Viewport Display sub-panel - under Display As a Wire is selected, and this is why Player looks underfed, sorry, wireframed. Since during the gameplay the Player is actually not visible, this will save some computer power. Not much though. :)

Of our main interest is a Head object, which is of type Empty object - those are commonly used as Parent object. In our case, Head is a parent of Camera object - this is the “eyes” of our player. No camera - no image on our screen.

Next, under the Camera, there is a Gun object, and this one has an Animation object with Action assigned to it.

Those are main building blocks of the game development, which we will incorporate into Logic Nodes workflow. Let the party begin!

Delete Text object (it was showing > W to exit Annotation tool), we do not need it. Switch to Logic Nodes workspace (top-most header). Arrange the editors as you see fit, the one we will use most is Logic Node Editor.

Tip

Both game and 3D model development require use of multiple editors at once. This can clutter the screen, particularly small one, quickly. To remedy this, few workflows are at our disposal.

  1. Dedicate each workspace, like default Blender startup file. In this case there are workspaces, which can be switched by clicking top-most header, in our example:

    Layout | Logic Nodes | Animation | Scripting

    There is also + tab, if you want to add another workspace. And you can assign hotkeys for switching previous/next workspace: Edit > Preferences > Keymap - search for Screen > Cycle Workspace. Assign 2 keyboard keys, one for previous and one for next workspace.

I trust you can figure out how to delete a workspace by yourself.

  1. Use Blender/UPBGE handy shortcut, Ctrl-Space, which will toggle-maximize the editor window under the mouse cursor. This was a preferred workflow of now late Johnny Maccarony, world-known heavy consumer of Logic Node noodles.

In Logic Node Editor, first create new logic tree, click Logic Node Editor, rename to player, and mark Protected (Fake User); next add basic movement nodes, same as for Moving A Cube chapter.

  • Add 5 Keyboard Key nodes, assign D A W S keys for right/left and forward/backward movement, and Space for jumping.

  • Add 2 Math nodes and connect with above 4 movement nodes.

  • Add Apply Force node, connect remaining Keyboard Key node If Pressed output socket to Condition input socket, add Player as object, set vector Z (up/down axis) to 200.0 - this much force will be applied. Local ? Judge yourself, or use time-tested very successful procedure - trial & error. Lucky you, just 2 options are available.

  • Add Combine XYZ node, connect above Math nodes. Leave Z at default 0.0 value.

  • Add Vector Math node, set to Normalize, connect those 2 cute blue-ish dots.

  • Add another Vector Math node, set Operation to Scale. This node will determine speed of the Player. Connect Result from above node to Vector 1 input socket.

Tip

If you want nodes to be aligned, turn on magnet icon in top-right corner of Logic Node Editor, above N-panel. Nodes will now snap to invisible grid. Hotkey is Shift-Tab.

../../../_images/ln-fps-speed_property-apply_tree.png

Add Game Property and Apply To Selected

Before we continue with nodes, we need to add Game Property. With Player selected:

  • In Properties > Game > Game Properties, click Add Game Property, name it speed, leave default Float type, and set value to 1.0.

Apply To Selected - this will create NL__player game property, player object tree, and a nl_player.py script. NL/nl stands for node logic.

Good job. Now we can continue with nodes.

  • Add Get Object Property node; if not already, select Game Property from dropdown, and in Property field type speed - this is the speed property that we just created above. Connect this node to Scale input socket of the last Vector Math node, and add Player as owner object - click Object and select from menu.

  • Add On Update and a Apply Movement node - connect Out to Condition, and last Vector Math into Vector; also set Player as object, and yes, check Local.

This is our node setup so far:

../../../_images/ln-fps-setup_so_far.png

Player movement nodes setup so far

Tip

Another very useful Blender habit to grow - almost all buttons & commands have option to Add to Quick Favorites or Assign Shortcut:

  • Go to Properties > Render > Game Resolution;

  • Hover mouse over Embedded Start button, RMB and select Add to Quick Favorites.

  • Use this tip whenever you find yourself using/repeating same actions over and over. It is a great time saver.

../../../_images/ln-fps-quick_favorites.png

Q for Quick Favorites > E for Start

According to the Holy Blender Bible, Embedded Start can be invoked with hotkey P, which seems to be not working in current UPBGE by default. So we added it into Quick Favorites, although we could as easily assign a P shortcut. We will now run game and test if node setup is working as expected. With mouse over 3D-Viewport:

Numpad 0 > Home > Q will move into ‘camera view’ > zoom-in/maximize/center the camera view > open Quick Favorites menu; there, another hotkey is offered for the Embedded Start - this depends on various factors, and is recognizable by any of the letters being underscored - E in figure; in your case might be something else. Pressing that key should start the game. Esc to end the game.

While the game is running, press movement keys and observe Player behavior.

  • First of all, it is moving way to fast > reduce the speed property; trial & error is the name of the game, until you find comfortable settings. Hint - halve the speed value for a start, and do not forget - Player should be selected in order to change its speed property.

  • Second of all, keys are wrong > fix them until character moves properly relative to pressed keys > W forward; S backward; A left; D right.

../../../_images/ln-fps-mouse_moves_head.png

Mouse Look settings

If you’re done fixing, let’s continue.

  • Add Mouse Look and connect Out from On Update to Condition, set Player as Object and Head as Head. Run the game, test, if needed, adjust settings in Mouse Look node: Cap Up/Down will limit Head movement, Smoothing will smooth mouse moves.

Next we upgrade walking to run:

  • Add/duplicate Keyboard Key & Math, add Value Switch. The logic here is:

    • When Shift is pressed, add some float value to speed, and use resulting value for movement;

    • If not pressed, use existing speed value.

Test the game, adjust values as you see fit.

../../../_images/ln-fps-value_switch.png

Value Switch for running

Now it is time for some actions - we’ll add gun shooting. Switch to Animation workspace, in the left editor/window > top-right, check Shading options - little arrow at far right side will dropdown some setting, which you are free and advised to change.

Tip

With object selected, you can ‘focus’ it with Numpad , (comma); this works in 3D Viewport, Outliner, and other editors. Use this if you ‘loose’ the object from sight.

../../../_images/ln-fps-action_editor.png

Action Editor context with action dots

  • Expand/enlarge bottom Dope Sheet Editor, select/expand Gun object (Outliner > Search if needed), and you shall see some orange dots in Dope Sheet - this is gun Action - when gun is fired, bullet pushes it back and up. Hit Space to toggle animation playing. We need to attach this action to Player and connect it with an input.

  • Switch Editing context from Dope Sheet to Action Editor - name of our action is shown in the header > Shot.

  • Back to Logic Nodes workspace (or modify workspace to include Logic Node Editor window/area). Add Mouse Button, Pulsify, Stop Animation and Play Animation nodes, connect them red-to-red dot.

  • Shot action is how long? Set Play Animation to Object > Gun, and Action > Shot, End > last action frame. Test the game, adjust settings.

Gun is moving when shooting now, but still needs to actually shoot something. And the sound is missing.

  • Add below Active Camera, Get World Position, and Get Axis Vector - connect Camera to both Get World Position > Object, and Get Axis Vector > Object, set Axis > -Z Axis (object/player forward-facing direction).

    • Get Axis Vector will take Active Camera orientation and feed it to Raycast for aiming direction.

    • Get World Position will take Active Camera (player body/head) position and feed it to Raycast as shooting origin point/position.

  • Add Raycast, connect Get World Position > Origin, Get Axis Vector > Aim, and above Stop Animation > Condition, check Custom Distance and Local.

  • Add Apply Impulse, connect Raycast top two output sockets color-to-color, and Picked Point > Vector, Ray Direction > Direction.

  • Add Draw node, set to Cube, connect Apply Impulse > Condition, Raycast Picked Point > Origin, set Width to something small, i.e. 0.02, and check Use Volume Origin.

    • Cube will act as improvised bullet. In final game this bullet would probably best be hidden.

Done - test and adjust settings.

../../../_images/ln-fps-shoot_nodes.png

Shot action nodes

../../../_images/ln-fps-shot_sound.png

Shot audio clip node

Final touch:

  • Search/download ‘machine gun shot single audio’, a short sound clip, save to disk, somewhere close to our working .blend file.

  • Next to above Play Animation add Start Sound, set as 2D Sample, connect Play Animation Started > Condition, Sound attribute > folder icon > load shot audio clip, note icon > load from dropdown. Test the game, take a break.