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:
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
return
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
return
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).
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.
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