A problem with the movement of a physical projectile

Hello guys!
I’m having a strange problem in my fps setup, a problem I can’t explain very well, here what happens:

Character stopped, shooting some cubes as bullets (no Ray sensor in this setup, only mouselook uses python):

the problem, when the character was moving, the projectiles didn’t look to have been created inside the gun, and didn’t walk in the direction of the aim too:

here what I want:

I put the setup attached, here the controls:

W/A/S/D = movement
Mouse = look/turn
Left mouse button = shoot
Space = jump
Ctrl = crouch

you guys have some solution for this? I see that the “Squish The Bunny” demo have the same problem with a rocket launcher

thanks and sorry my horrible english :o


problem.blend (546 KB)

You need to add the object’s velocity to the object’s motion. So add a Python controller (and remove the addObject modifier).

from bge import logic

ticRate = logic.getLogicTicRate()
bullet_name = 'bullet'
time = 100

def fireBullet(cont):
    if not cont.sensors[0].positive: # deactivate the negative pulse
    gun = cont.owner
    player = gun.parent.parent # dynamic object to get the velocity from
    scene = gun.scene
    bullet = scene.addObject(bullet_name, gun, time)
    bullet.worldPosition += player.worldLinearVelocity / ticRate

Edit2: My apologies for messing up the first time. So replace the And controller with a Python controller and set the Execution Method to Module. Then the name of the script + dot + the name of the module. For example: Gun.fireBullet. Don’t forget to give the script a *.py extension. So: Gun.py.

As a note. If you run a script with a Keyboard or Mouse Sensor it will run twice! The first pulse is a positive pulse, when the key/button is pressed. The second is a negative pulse, when the key/button is released. Of course you don’t want to shoot two bullets with every mouse click. That’s why on top of the script these to lines are added:

    if not cont.sensors[0].positive: # deactivate the negative pulse

In other words, if the left button is released (and gives a negative pulse), you will return (don’t run the rest of the code).

thanks for reply and the help Raco! ^^

well… is a bit embarrassing but… I could not make your code works here, sorry :o

can you post how to apply the code to the scheme?

(I’m kinda dumb with python and how put code to work with the logic bricks :spin:)

thanks again for your help

Oh, man, I’m sorry. I totally messed up. I deleted a couple of lines by mistake. I’ve changed the code.

Edit: Also, if I can suggest something. It will look better if you would set the bullet to Static Ghost and move it with Simple Motion. It depends on what your projectiles are, but looking at your file I think this is what you want. (It also makes the game faster). Here’s the blend: http://www.pasteall.org/blend/22177.

thanks Raco, this is just perfect! :yes:

the initial idea was to make a gun that a player can see the projectile flying, but now with your code I think it’s possible to substitute the ray sensor of ‘normal guns’ too, I just have to increase the velocity of the bullet (and the bullet still can be seen hit a region inside the square)

and thanks again! this code is really helpful ! =D