your problem is inhere:
def CameraShott(cont):
own = cont.owner
mlb = cont.sensors['MouseLB']
if mlb.positive:
start = own.worldPosition
end = own.worldPosition + own.getAxisVect([0.0, 0.0, -500.0])
ray = own.rayCast(end, start, 0.0, '')
hit = ray[0]
if hit:
if 'health' in hit:
hit['health'] -= 5
In very simple words:
It only works when you click the mouse button.
The health check is done when mlb.positive. So health is never being removed unless you click on the exact same time as the bullet hits the target.
//edit
Here is my origional bullet script. Remove everything you have that removes health/deals damage. and put this script on the bullets
#######################################
# Bullet script by Cotaks #
# bullet drop, speed, damage and hole #
#######################################
# put this on the bullet, always(true) -> python-module -> scriptname.bullet_action
# put a property health on any object.
# Done!
from bge import logic
def bullet_action(cont):
own = cont.owner
speed = 340
damage = 2
bullet_drop = 0.0015 # higher means faster drop
hole = 'plasma_hole' #name of the bullethole plane
distance = speed/logic.getLogicTicRate()
own.applyMovement([0,distance,-bullet_drop], 1)
y_vec = own.worldOrientation.copy().col[1]
y_vec.magnitude = distance
end_vec = own.worldPosition.copy() + y_vec
ray = own.rayCast(end_vec, own.worldPosition.copy(), 0)
if ray[0]:
if 'health' in ray[0]:
ray[0]['health'] -= damage
if ray[0]['health'] <= 0:
ray[0].endObject()
bullet_hole(cont,ray,hole)
def bullet_hole(cont,ray,bullet_hole):
own = cont.owner
own.endObject()
#300 is the duration timer (second * 60 so 5 seconds)
bullet_hole = own.scene.addObject(bullet_hole, ray[0], 300)
offset = ray[2]
offset.magnitude = 0.001
bullet_hole.alignAxisToVect(ray[2], 2, 1)
bullet_hole.worldPosition = ray[1] + offset
bullet_hole.setParent(ray[0])
if you don’t want a bullet hole, then remove every line with hole in it and that function bullet_hole completely.
Now you have real speed and calcualtions, health will be removed, now it’s up to you to figure out how you want to change the scene.
//edit 2
here a blend file, run it and hit spacebar to shoot, 5 times to kill the cube.
bullet script preview.blend (575.5 KB)
(upbge 0.2.5)