Hey guys,
I'm working on rewriting certain parts of my FPS engine, and i've stumbled across a few problems in writing up some decent code for shooting. Ever since I modified the mouselook/player movement, the shooting has been acting strange.
When I click to fire, the bullet is positioned at the camera (player) position, then pointed straight ahead using the
set object to camera orientation command. Then, I set a flag to one, and if that flag is equal to 1, i'll move the bullet using the MOVE OBJECT command. When the bullet is a certain distance away, the flag is set to 0 and hidden. The problem is that the X, Y, and Z position of the bullet is constantly decreasing as soon as the bullet is fired, rather than just moving in a perfectly straight line, straight ahead from where i'm looking.
My movement code:
REM Controls for moving the player
tkeystate30=0 : tkeystate32=0 : tkeystate17=0 : tkeystate31=0
IF KEYSTATE(17) THEN tkeystate17=1
IF KEYSTATE(31) THEN tkeystate31=1
IF KEYSTATE(30) THEN tkeystate30=1
IF KEYSTATE(32) THEN tkeystate32=1
REM Camera (old player position) information
cox#=CAMERA POSITION X()
coy#=CAMERA POSITION Y()
coz#=CAMERA POSITION Z()
REM Control camera movement
movement=0 : speed#=3.0
x#=CAMERA ANGLE X() : z#=CAMERA ANGLE Z() : sy#=CAMERA ANGLE Y() : y#=sy#
IF tkeystate30=1 THEN DEC y#,90 : movement=1
IF tkeystate32=1 THEN INC y#,90 : movement=1
ROTATE CAMERA 0,y#,0
IF tkeystate17=1 OR tkeystate30=1 or tkeystate32=1 then MOVE CAMERA speed# : movement=1
IF tkeystate31=1 THEN MOVE CAMERA speed#*-1.0 : movement=1
ROTATE CAMERA x#,sy#,z#
rem Camera new information
cmx#=CAMERA POSITION X()
cmy#=CAMERA POSITION Y()-grav#
cmz#=CAMERA POSITION Z()
rem Overall ellipse collision for camera
IF STATIC VOLUME(cox#,coy#-20,coz#,cmx#,cmy#-20,cmz#,1.0)=1
materialtype=GET STATIC COLLISION VALUE()
cmx#=cox#+GET STATIC COLLISION X()
cmy#=coy#+GET STATIC COLLISION Y()
cmz#=coz#+GET STATIC COLLISION Z()
grav#=0.5
ELSE
INC grav#,0.5
ENDIF
REM Stop falling forever, stops at -200 currently.
IF cmy#<-200.0 then cmy#=-200
rem Update camera position
SET POINT LIGHT 0,cmx#,cmy#,cmz#
My mouselook code:
rem Control camera view
camangx#=camera angle x()+cammovey#/1.5
camangy#=camera angle y()+cammovex#/1.5
if wrapvalue(camangx#)>85 and wrapvalue(camangx#)<180 then camangx#=85.0
if wrapvalue(camangx#)>180 and wrapvalue(camangx#)<275 then camangx#=275.0
rotate camera camangx#,camangy#,camera angle z()
rotate object player,camangx#,camangy#,camera angle z()
POSITION CAMERA cmx#,cmy#,cmz#
POSITION OBJECT player,cmx#,cmy#,cmz#
My shooting code:
if mouseclick() = 1 THEN bulletfiredaway = 0
if bulletfiredaway = 1
show object bulletobj
position object bulletobj,camera position x(),camera position y(),camera position z()
set object to camera orientation bulletobj
shot = 1
endif
if shot=1 and moving = 0
bulletfiredaway = 0
moving = 1
set object to camera orientation bulletobj
endif
if moving = 1 then move object bulletobj,50
if object position x(bulletobj) > 10000
shot = 0
endif
Any help with getting the bullet to just move in a straight line in front of me rather than moving on all 3 axes would be great. Thanks.