Also, very glitchy, i could give you my template script.
mainly, though i think you need to change your ray script and i think its the auto reload your seeing (empty clip, if fire: reload).
Add empty called spawner to end of gun
add always, and add ray (called ray) sensor to spawner both with true pulse (ray sensor set range along axis which points out the gun away from you)
add another empty "in" the gun, called coll
in gamelogic, add an always sensor called coll with true pulse
use mouse lclick sensor "fire"
keyboard "reload"
now on spawner in gamelogic, add a python controller to python script spawnbul.py (add in a min)
now connect coll, keyboard run ("called run") sensor, keyboard "reload" sensor, to python script bullet.py
create two text files: one called spawnbul.py, and one called bullet.py
(spawnbul)
################################################################################
# #
# This script has been written regarding #
# to a problem I noticed about blender. #
# #
# #
# First, you got to know that when you create an object, you need to #
# specify "where"... which isn't a problem cuz usually, when you create #
# something, you have a good idea of where you want it to be. The #
# "spawning location", which is where your object will appear in the #
# game, is usually the center of another object (usually an empty for #
# "no-collision" purposes). When you do that, you tell blender to seek #
# for the center of the empty (Or the kind of object you choose) and #
# create your object there. Problem is that blender does have a #
# little "gap"... meaning that he will look for the location of the #
# object "FROM LAST FRAME". Therefore, blender will create your object #
# where your spawner* was one frame before. Since there's about 60 frames #
# each seconds, this gap doesn't seem big... but it is : A gap of one #
# frame from a spawner moving at 10 blender unit (meters) per second will #
# get a gap of 10/60 = .166 meters = 17 centimeters = bullet spawn way #
# off from the weapon and the player is not happy.... nor as the programmer. #
# #
# #
# To avoid this, I tried many way... but nothing simple seem to have any #
# impact on this problem : it's just the way blender think. So, to get rid #
# of this problem, I just offset the "spawner" from a distance relative #
# from the actual velocity of the spawner's parent. Therefore, the spawner's #
# position is approximately one frame ahead. So, if blender only consider #
# the position of the spawner one frame late, it's gonna be the actual #
# frame. And yes, the trick work. BUT, there is still a gap between the #
# created object and it's supposed location. This is due to the fact that #
# the approximation used to move the spawner one frame ahead does not take #
# the acceleration in consideration. Thus, the approximation would gain lot #
# of precision by considering the acceleration, but relating to the fact that #
# blender doesn't seem to already calculate it (not like the velocity), #
# there would be a need to calculate the acceleration from the 3 precedents #
# positions. These calculations could slow down the game and I will avoid #
# including them to the script as long as the gap isn't to bad... or until #
# I discover that the requested calculation power isn't that big. #
# #
# #
# Since the spawning point of the bullet may be set randomly anyway, #
# no need to get crazy about the exact supposed position of the #
# spawner : 10 centimeters off the weapon is quite too much, but #
# a few millimeters is pretty much reasonnable #
# #
################################################################################
##########################
# #
# Initialization routine ################################
# #
##########################
# get the spawner's reference name
spawner = GameLogic.getCurrentController().owner
# Get properties
init_prop = spawner['Init']
locPos_prop = spawner['Local_Pos']
if init_prop == 0:
spawner['Local_Pos'] = spawner.localPosition
spawner['Init'] = 1
#######################################
# #
# *** ENOUGH TALK *** ###################
# Time for the real stuff ;) ###################
# #
#######################################
FRAME_RATE = 60
new_pos = [0,0,0]
for i in range (0,3):
new_pos[i] = spawner['Local_Pos'][i] + (spawner.parent.parent.parent.getLinearVelocity(1)[i] / FRAME_RATE)
spawner.localPosition = new_pos
(bullet,py)
import random
import math
# Get controllers
controller = GameLogic.getCurrentController()
# get the spawner's reference name
spawner = controller.owner
target_colider = controller.sensors["coll"].owner
# Get sensors
ray = controller.sensors["ray"]
running = controller.sensors["run"].positive
fired = controller.sensors["fire"].positive
reload = controller.sensors["reload"].positive
# Get actuators
recoil = controller.actuators["play_recoil"]
#define variables
isFiring = False
ammo = getattr(controller.owner, "ammo")
clips = getattr(controller.owner, "clips")
canfire = True
#define event
if reload:
if clips > 0:
if ammo < 1:
setattr(controller.owner, "clips", clips - 1)
setattr(controller.owner, "ammo", 200)
if fired:
if not running:
if canfire == True:
if ammo > 0:
GameLogic.addActiveActuator(recoil, 1)
setattr(controller.owner, "ammo", ammo -1)
if ray.positive:
# Position & orient the "target collider" #
###############################################
#target_colider.worldPosition = ray.hitPosition
#target_colider.worldOrientation = spawner.worldOrientation
# Make the hit object to loose health #
###########################################
object_hit = ray.hitObject
try:
new_health = object_hit['Health'] - 9
except:
pass
else:
if new_health > 0:
object_hit['Health'] = new_health
else:
object_hit.endObject()
# Affect the object's velocity & torque #
#############################################
# Set the bullet's force applied in one frame (In newtons)
bullet_force = 200
try:
velocity_obey_prop = object_hit['Velocity_Obey']
except:
pass
else:
if velocity_obey_prop > 0:
force_x = -spawner.worldOrientation[1][0] * (1-abs(spawner.worldOrientation[2][1])) * bullet_force * velocity_obey_prop
force_y = spawner.worldOrientation[0][0] * (1-abs(spawner.worldOrientation[2][1])) * bullet_force * velocity_obey_prop
force_z = spawner.worldOrientation[2][1] * bullet_force * velocity_obey_prop
force = (force_x,force_y,force_z)
object_hit.applyForce(force, 0)
try:
torc_obey_prop = object_hit['Torc_Obey']
except:
pass
else:
if torc_obey_prop > 0:
force_x = -spawner.worldOrientation[1][0] * (1-abs(spawner.worldOrientation[2][1])) * bullet_force * velocity_obey_prop
force_y = spawner.worldOrientation[0][0] * (1-abs(spawner.worldOrientation[2][1])) * bullet_force * velocity_obey_prop
force_z = spawner.worldOrientation[2][1] * bullet_force * velocity_obey_prop
dx = ray.hitPosition[0] - object_hit.worldPosition[0]
dy = ray.hitPosition[1] - object_hit.worldPosition[1]
dz = ray.hitPosition[2] - object_hit.worldPosition[2]
tx = -(force_y * dz) + (force_z * dy)
ty = (force_x * dz) - (force_z * dx)
tz = -(force_x * dy) + (force_y * dx)
torque = (tx,ty,tz)
object_hit.applyTorque(torque, 0)
please read next post also as char limit