Need help making a gun shoot

I’ve been having a lot of trouble with a shooter that I’ve been making recently in Blender 2.57Beta. For some reason I can’t seem to make a gun in my game shoot. I mean, it has the recoil and the sound, but I don’t know how to make it hurt the enemies. Can someone please tell me how I can make this gun hurt the enemies without using python? Any help is greatly appreciated! Thanks!

In the logic for your enemys use a collision sensor, an and controller , and an edit property actuator.

On the collision sensor make it only go off on collision with an object that has a property - string - HITENEMY
For the property actuator - prop: HEALTH , add and VALUE: -25 or whatever health you want the enemy to take. You’ll also need to have a property on your enemy: HEALTH - integer - 1000. Or whatever his health is.

Now on the object that your gun shoots add a property named HITENEMY
And now whenever your enemy gets hit with your projectile it’ll lose whatever you set the VALUE to in the prop actuator.

And if your gun does not physically shoot an object then my way will not work.

Now if you need to know how to make your gun actually shoot an object. Go to the logic of your gun , keyboard sensor an and controller and edit object, add object, object is whatever object you want as long as its dynamic. Also the object that you use as a projectile MUST be on a different layer than your gun.

Creating an object to make your gun fire is inadvisable because it will slow down the game engine. And the objects (when moving fast enough) are known to go through objects without triggering a sensor.

It is possible to do the shooting without python but it will get complicated fast. You could add a ray sensor that reacted to EnemyNo1 and then send a message to a pre-programmed object (EnemyNo1.) But You’ll have to write in the objects in the “To” field on the message sensor. You’ll have tons of unique ray sensors and unique message sensors. Fine if you have two or three objects, not a good idea for a large game.

If you use Python you can grab the name of the object your shooting at with the hitObject sensor and slap it onto the message sensor. All you would need is two sensors (fire button, and ray sensor), one python controller, and one message sensor.

Okay, I think I’ll do it in Python. Can you please point me to a tutorial?

so did you ever find a tutorial to do this python stuff?

Oh sorry, must have missed the post.

You need a ray sensor, a mouse sensor, and a python controller.

if mouse_left_click.positive and own["ammo"] > 0:
    #I define the ray sensor here, because otherwise the script activates every
    #time the ray sees an object with the right property
    ray_sensor = cont.sensors["ray_sensor"]
    #add the muzzle flash
    #play the kick action
    #one bullet is fired
    own["ammo"] -= 1
    #if there is an enemy in your sights
    if see.positive:
        #get the enemy you see
        otherplayer = see.hitObject
        #decrease the health of the enemy by the power of the gun
        otherplayer["health"] -= own["gun_power"]

I’ve heard about problems with spawned objects having the same name. For example: if you had an empty using the “add object” actuator to spawn enemies. One shot might damage all spawned enemies because they all have the same name.
I have not gotten that far in my testing, but if you have that error I’ll try and fix it.

cool! Thanks. I just started learning about bge today and so don’t know anything at all but infininte talked about the other ways would slow down the game engine so I figured if I am going to learn how to make a gun shoot, then I should learn a way that doesn’t slow down bge. It might be awhile be fore I can figure out what you are talking about. I have watched some tutorials on youtube today and tried to follow along but so far no luck in makeing a gun shoot but I know it’s not far ahead of me.
Anyway i am going to hang on to what you posted and someday soon I will know what you are talking about.

If your shooting something slow, like a laser, you’ll want to create the object. But otherwise it is easiest to use a ray sensor and some python.

This code is very simple.

In the first step I ask if your clicking the left mouse button and if you have bullets to fire

if mouse_left_click.positive and own["ammo"] > 0:

Next I ask the game engine to give me the ray sensor so I can use it.

ray_sensor = cont.sensors["ray_sensor"]

Then I show the muzzle flash from firing the gun,


show the gun kicking back from the shot,


and I remove one bullet from the clip

own["ammo"] -= 1

The first part will happen if you have ammo left, but the last part (actually hurting an enemy) only happens if there is an enemy in your sights.
I ask the game engine if there is an enemy in your sights.

if see.positive:

There is? Well swell. But now I need the enemies name.

otherplayer = see.hitObject

Now that I know his name I can remove some of his health.

otherplayer["health"] -= own["gun_power"]

If you want to make a simpler version that only requires logic bricks that is possible too. The disadvantage to using logic bricks though is that it gets really complicated really fast. But if you have only one or two enemies logic bricks will do just fine.

Create a ray sensor that will be triggered by a unique property in one of your enemies.
Attach this to a message actuator through a “and” controller.
In the “to” field put the name of the enemy with the unique property.
In the subject field put “-5” or however much health you want to take away.

In the enemies (the logic bricks that govern being hurt do not have to be unique) create a message sensor.
In the subject field put “-5” or whatever subject you put in the message actuator before.
Attach the message sensor to a property actuator through an “and” controller.
The property actuator needs to be the type “add”
Set the property field “health” or whatever property you have that governs health.
Set the amount to -5 or however much you want to take from the enemies health. (Note: the number you put in has to be negative or else you will just be adding to the enemies health.)

I hope that helps. If it was confusing don’t worry, read some tutorials and you should be understand the logic brick way of shooting completely.