Sie sind auf Seite 1von 28

Part three of a series of tutorials for the Blender game engine by Smoking_mirror.

Document Description: This is part three of a set of tutorials, it covers setting up a helicopter type character's weapons for use in the BGE*. * Blender Game engine

Document Aim: The aim of the tutorials is to introduce new users of the BGE to some of the concepts needed to produce a moderately complex game. The aim of this third tutorial is to show users how to set up their player character's weapons. A similar set up can e used for the enemy weapons Document Goals: !escri e how to ma"e a weapon's effects o #ect. $how how to integrate the shooting script. $how how to set up the ground and enemies so they can e damaged and register hits. $how how to ma"e the first steps of a asic %&! 'heads up display, or &(, user interface)

Introduction: *e've already made a game model and set up its movement profile, now we are getting to the fun part. (n this tutorial ('m going to show how to add weapons to your character. Again, ('m going to e supplying the scripts, all you have to do is set up the game o #ects as shown and plug in the scripts to your lend file y cutting and pasting.

Document Inde : Part ! " #reating the $eapon effects. Part & " Setting up the player logic and ob'ects. Part * " Adding a +,D Part % " Setting up the ground and enemies for collisions. " Page % ( Page !) ( page &( page &)

After$ord

" page &.

Part /ne ( #reating the $eapon effects: *e are going to give our character + different "inds of weapons. The original !esert $tri"e only had ,, ut we want to add more tactical challenge to the game y including anti-missile flares. The first weapon is the .achine gun, or ,/mm cannon as it will e called in the %&!.

*hen creating the weapons effects we have two options. 0ne is to use a ill oard type o #ect, a single plane which always faces the camera and loo"s the same from every angle. The other is to use , planes set up li"e the gun flash a ove. This has the advantage of eing a le to loo" different from different angles. 1ou should choose which to use in each case. A missile tail should use , planes, while a small explosion might use the single ill oard. There are also different types of transparency you can use. 2Alpha clip3 and 2alpha lend3 re4uire an alpha channel on your image. 2Add3 #ust re4uires that the ac"ground, and any part you want to e invisi le e lac". 5or these ones we are going to use 2add3 type transparency. This loo"s est with the , plane type o #ect.

$o add a material to your weapon effect. Be sure to chec" the shadeless ox and set the game settings. *hen you add a texture don't forget to set the &6 mapping7

%ow you set up your materials and textures will depend on whether you are using the G8$8 mode or not.

1ou should go ahead and ma"e all the weapons effects. 1ou can also use animated textures as ('ve done with the 2 ig9 ang3 effect7

(n the image window press 2n3 to open the option panel and set the game properties. *hen ma"ing an animated texture you should have a num er of s4uares which can fit e4ually in to the si:e of the image. $o if you have a ;<=x;<= image, +x+ animations wor" well, ut =x= or >x> will give a strange effect.

*e need the 2gunflash3 o #ect as well as all the other pictured a ove. (f the o #ect is not going to have physics 'set to 2no collision3) you #ust need the mesh. (f it is going to have collisions it's etter to parent the graphics mesh to a collision mesh. This can e a simple cu e or rectangle, set to 2solid ody3 and with the invisi le and ghost oxes chec"ed. This allows us to etter control the collision. $ee the missile a ove, we only want the missile, not its tail, to register collisions. The oxes at the top are empties set up to carry a sound.

1ou can set up the sound logic li"e this. 0pen the .wav file you've prepared, chec" the ,d sound ox and set the volume to whatever sounds right in game. 1ou need the following sound empties7 1ou can also add the helicopter lades sound to your player o #ect. ?ust give it an always sensor and an A@! controller.

1ou will need 4uite a few sounds for your pro#ect. http7AAwww.freesound.org is a good place to loo" for sounds.

This list should e enough though we may need to add more. ( will include these sounds with the Blend files, they will e pac"ed along with the textures.

Going ac" to the weapon effects, we need to set up the actions for the explosions. Add "ey frames to set the scale of the explosion 'see tutorial ; for this info) and then go to the o #ect display settings. 1ou can set the o #ect color here, which will e used in the action to change the color of the explosion, ma"ing it fade out over time.

Bight clic" on the o #ect color ox to add a "eyframe.

1ou need to ma"e an action li"e this. (t gets igger and fades out over time.

$et up the logic li"e this. 5or the roc"ets and missiles you will have to add two properties7

p9 ullet means that this o #ect will e detected y enemy chec"ing to see if they have een hit. %it type sets the damage level and ma"es the o #ect e affected y flares 'if hit type ;). Boc"ets should e hit9type C while missiles will e hit9type ;.

1our missiles will also need some logic to ma"e them trac" to your target. 8eave the trac"ing o #ect ox empty, it will e set y python.

1ou also need some ullet hole type o #ects. They should lay flat on the ground.

1ou need 2m9hole3, 2r9hole3 and 2 9hole3. They should e set to no collisions and they don't need any special logic. (n other games we might give them an animation to ma"e their appearance more natural, ut here they are going to e too small to notice. $o the o #ects you need are as follows7
@ame 2gunflash3 2player9roc"et3 2player9missile3 2flare3 2m9hole3 2r9hole3 2 9hole3 2small9 ang3 2 ig9 ang3 2mg9sound3 2roc"et9sound3 2missile9sound3 2missile9hit3 2no9ammo3 Type *eapon effect *eapon effect *eapon effect *eapon effect *eapon effect *eapon effect *eapon effect *eapon effect *eapon effect *eapon sound *eapon sound *eapon sound *eapon sound *eapon sound Dhysics @o collisions $olid ody, ghost $olid ody, ghost $olid ody, ghost @o collisions @o collisions @o collisions @o collisions @o collisions @o collisions @o collisions @o collisions @o collisions @o collisions Droperties @one 2hit9type3 intEC, 2p9 ullet3 oolETrue 2hit9type3 intE;, 2p9 ullet3 oolETrue 2flare3 oolETrue @one @one @one @one @one @one @one @one @one @one

1ou can set up the flare o #ect to have animations, #ust li"e the explosions, ut you need a slightly different set of animation curves7

Part t$o ( Setting up the player logic and ob'ects: *e want to add some empties to our player o #ects, we need three empties called...

...roc"ets9right, roc"ets9left and m9gun. The empties are where the weapons are going to e fired from. Darent them to the player mesh, or to the roll action o #ect 'the circle).

*e also need an empty set way out in front of the chopper called 2null9target3.

This also needs a property called 2null9target3.

(t's est to set his o #ect a little lower than your chopper and parent it to the roll action o #ect. This is what your guns will aim at if they have no enemy target, so you #ust want them to fire at the floor a little way in front of the helicopter.

@ext we have to give our player o #ect some new properties. $et them up #ust li"e in the picture a ove. 2target9o 3 and 2enemy3 are oth strings, they can store any information here, in this case we are going to e storing references to o #ects in game. 2target9o 3 will e the 2null9target3 o #ect we created a ove, while enemy will e the closest enemy to the player, if they are in the correct arc.

This is the logic set up. (t has #ust two sensors and three actuators.

Be sure to add each actuator and sensor as shown. 1ou can increase or reduce the num er of logic tic"s on the left mouse utton sensor to get faster or slow firing weapons. The python module is7 cobra.cobra_shooting At this point you should get the new script from the place you got this tutorial. ?ust cut and paste it over the old co ra.py script in your text window. All the new code has een added to this script. 1ou may have already added the helicopter sound in the last section. ?ust connect it to the same always sensor as the helicopter lades. *e will e changing that in future, ut for now it's nice to hear the 2whup-whup3 sound as your chopper flies around the screen.

Part three ( Adding a +,D: There are several ways to handle adding a %&! or &( to your games. 0ne of the most common is to add a overlay scene. %owever we are going to "eep things simple and simply add some o #ects to the scene parented to our camera.

Add a simple plane to the scene and parent it to the camera so its rotation matches the camera.

Ad#ust the position in the view so it is in the ottom right hand corner.

Give the o #ect a property called 2amount3. Freate a new action for the weapon display o #ect to set the o #ect color 'li"e we did with the explosions and flare a ove).

@ext we need to add some more properties to the camera7

And we also need to add some more actuators to the camera and display o #ect7

.a"e sure you set the action to 2property3 not 2play3. The actions should e 2display switch3 and 2color9code3. *e want the player to see if they are running out of ammo, ut we don't want to get in to using text yet, so we are going to show ammo levels y color. 2Amount3 is the property which drives the color change of the weapon display o #ect. (t has to e on the o #ect, not on the cam.

Freate four o #ects on another layer, you will need a new texture for this, ('m using this one7

The arrow and power ar will e used later to show fuel, armor and nearest enemies and o #ectives. @ame the meshes of the o #ects as follows7
@ame7
$eapon_display_! $eapon_display_& $eapon_display_* $eapon_display_%

Bepresents7 .achine gun Boc"ets .issiles 5lares

1ou should e careful how you set up the o #ect color action. (t should fade from pale green at / frames towards dar" red at C// frames and then lac" at C/C frames. This should give the following result7

*hen you are out of ammo the image goes lac". 1ou should also create a lan" mesh 'move the verts on the &6 image so they are covering a transparent part of the image) and call it 2empty9display3. This will e displayed if your chopper has no weapons. (f you did everything correctly you should now e a le to fly around and fire your missiles, ut they don't explode. $o how do we handle thatG

Part four ( Setting up the ground and enemies for collisions: Add some cu es to your scene. .a"e them static and give them the following properties7

@ext set up their logic li"e this7

The python module is7 cobra.player_weapon_hit and the collision sensor should e chec"ing for the property 2p9 ullet3 'remem er it's the property you gave the player's missiles and roc"ets earlier).

!o the same with the ground in your scene7

But don't give the ground the 2health3 property, instead it needs the 2ground3 property. 1ou will need to give all enemies and uildings this set up if you want them to e shoota le. 0 #ects with the 2ground3 property will accept missile and roc"et craters. 1ou don't want to give this property to small o #ects such as flagpoles and people ecause they are smaller than the crater and it'll loo" li"e the crater is floating in mid air. (nstead use it on things li"e large uildings. @ow when you fire your weapons the missiles and cannon should automatically trac" the enemy o #ects, and they should register explosions. (f you cause enough damage you can destroy the enemy o #ects. 8ater we are going to set up enemy so they explode when destroyed, ut for now have fun firing your weapons.

I01e added a ne$ player model to the blend file that goes $ith this tutorial. If you change the player ob'ect adder to add 2player_alt3 you can en'oy flying around in a classic ,+"! +uey. 4his $ill be part of a later tutorial focusing on changing the player ob'ect to add more 1ariety to missions 5in the ,+"! you $ill be able to pick up passengers6. 7e t time $e $ill look at making enemies and gi1ing them simple AI. 4hanks for reading. If you ha1e any 8uestions please post them in the Blender artists thread. Smoking 9irror A:A Pickledte;cat A:A <ohn 4opple. *th

<une &-!*

(f you want to write your own tutorials to e included in the series you should use the following formatting conventions7 .ain Title7 $egoe Drint ;= point, Fentered. $u titles7 $egoe Drint C= point, left aligned. .ain text7 $egoe &( $emi old C= point, left aligned. (mage captions7 $egoe Drint C= point, Fentered. The document should e landscape, not portrait for etter viewing on wide screen monitors without splitting pages, and images included should e of a maximum with of <C; pixels and should e centered. There should e a roughly </A</ mix of text and images 1ou can use the following image for title text ac"ground7

(t's a simple gradient from pure white to 2+/+/+/3 gray on the right. =rom no$ on all the tutorials can be found here: Tutorials y smo"ing .irror

Das könnte Ihnen auch gefallen