Hello, and thank you for taking the time to help me.
What I want the code to do is:
To take a snapshot where the player is, then shoot a bullet at it, if it hits good, and if it doesn't make the bullet come back to the enemy. But my code is not letting the bullet get to the player's position and it is a little bit off and sometimes gets stuck the player(you) moves diagonally. You can see better the effect if you stop and go, stop and go.
This is my code:
` set up display
sync on
sync rate 30
hide mouse
`make reference matrix
make matrix 1,40,40,5,5
`load player
make object cylinder 10,1
position object 10,20,1,4
`load enemy
make object cylinder 13,1
position object 13,20,1,32
`make player's bullet
make object sphere 15,0.8
color object 15,rgb(255,0,0)
position object 15,20,1,35
hide object 15
` position and point camera
position camera 20,10,-25
point camera 20,4,40
` initialize variables for player
playerx#=20 : playerz#=7
enemyx#=20:enemyy#=1:enemyz#=32
` initialize other variables
bulletlife=0
hit=0
ebulletlife=0
ehit=0
`main program
do
gosub playermovements
`collision system for enemy
gosub enemycollisionsystem
`enemy A.I.
gosub enemyai
position object 10,playerx#,1,playerz#
sync
loop
playermovements:
` player simple movements
if upkey()=1 then playerz#=playerz#+0.20
if downkey()=1 then playerz#=playerz#-0.20
if rightkey()=1 then playerx#=playerx#+0.20
if leftkey()=1 then playerx#=playerx#-0.20
return
`collision system for enemy
enemycollisionsystem:
`check if there is collision enemy's bullet with player
collision_ebullet_with_player=object hit(10,15)
`if there is collision enemy's bullet against player
if collision_ebullet_with_player=1
ehit=1
endif
`check collision enemy with enemy bullet
collision_enemy_with_ebullet=object hit(15,13)
`if enemy bullet collides with enemy
if ehit=1 and collision_enemy_with_ebullet=1
ehit=0
ebulletlife=0
endif
if object position z(15)<=object position z(10)
ehit=1
endif
return
` Enemy's A.I.
enemyai:
`take desition about what the enemy is going to do
if ebulletlife=0 and ehit=0 then desition=rnd(2)
`desition #1
if desition=1
` bullet creation for enemy
if ebulletlife=0 and ehit=0
`snapshot the player's position
playersnapx#=object position x(10)
playersnapy#=object position y(10)
playersnapz#=object position z(10)
` position bullet in the gun
position object 15,enemyx#,enemyy#,enemyz#
set object to object orientation 15,10
`bullet is alive
ebulletlife=1
show object 15
`reset the FLAG value when you shoot, just in case
ehit=0
endif
`bullet path
if ebulletlife>0
move object 15,0.9
`if the bullet has yet to hit the target, then make it point towards it
if ehit=0
point object 15,playersnapx#,playersnapy#,playersnapz#
endif
`if the bullet has already hit the target, make it point towards enemy
if ehit=1
point object 15,object position x(13),object position y(13),object position z(13)
endif
endif
`hide the bullet if it is not supposed to be visible
if ebulletlife=0 then hide object 15
` enemy's bullet bounds
if object position x(15)<=0 then ehit=1
if object position x(15)>=40 then ehit=1
if object position z(15)<=0 then ehit=1
if object position z(15)>=40 then ehit=1
endif
if desition=2
a=a+1: print a
endif
return