You were so close to doing the movement a really good way. Again we don't need to get the camera position because we are controlling it.
`MOVE CAMERA FORWARDS
if keystate(key_fwd)
XTest# = Newxvalue(X#,CamAngY#,10)
ZTest# = Newzvalue(Z#,CamAngX#,10)
if XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
move camera 10
X# = XTest#
Z# = ZTest#
endif
endif
...
X# = camera position x()
Z# = camera position z()
GH# = get ground height(1,X#,Z#)
position camera X#,GH#+35,Z#
position object 2,camera position x(),camera position y(),camera position z()
Could be:
`MOVE CAMERA FORWARDS
if keystate(key_fwd)
XTest# = Newxvalue(X#,CamAngY#,10)
ZTest# = Newzvalue(Z#,CamAngY#,10)
if XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
X# = XTest#
Z# = ZTest#
endif
endif
...
GH# = get ground height(1,X#,Z#)
position camera X#,GH#+35,Z#
Note that both the new X and Z values use the CamAngY, both positions are on the same horizontal plane, moving along the plane is only affected by Y angle, no XZ rotation is required to move along a horizontal plane. If there were hills or you could fly the movement would be more complicated and likely include both X and Z rotation.
You can combine the forward and back movement as they deal with the same axis of movement.
move = keystate(key_fwd) - keystate(key_bck)
if move <> 0
XTest# = Newxvalue(XTest#, CamAngY#, move*WlkSpd#)
ZTest# = Newzvalue(ZTest#, CamAngY#, move*WlkSpd#)
endif
Are you familiar with the keystate command? key_fwd and key_bck are just variables currently assigned 17 and 31 (W and S keys). WlkSpd# is another variable that holds the walking speed value for the player. It's good to set this sort of thing with a variable in case you want to change the value, you should have very few or even no bare numbers in your code if you want it to be easier to edit and read.
You can combine the strafing too.
I did a bit more editing to adjust the player's speed when strafing and walking at the same time -- he was moving twice as quickly since both actions move at full speed. I actually made an error here, not a fatal one but I could have done it better. Here's the whore subroutine (I changed it from a function so passing variables is easier), see if you can tell what could have been done better. Hint: Look for similar bits of code that could be combined, and about what direction the player is moving.
camera_control:
`Camera look
mmx = mousemovex()*2
mmy = mousemovey()*2
CamAngX# = CurveAngle(CamAngX#+mmy, CamAngX#, CamRotSpd#)
CamAngY# = CurveAngle(CamAngY#+mmx, CamAngY#, CamRotSpd#)
Xrotate camera CamAngX#
Yrotate camera CamAngY#
rem Store temp position for calculations
XTest# = X#
ZTest# = Z#
rem Store inputs for movement and halve speed if moving and strafing.
move = keystate(key_fwd) - keystate(key_bck)
strafe = keystate(key_rgt) - keystate(key_lft)
if move <> 0 and strafe <> 0
Stfspd# = Strafespd# / 2.0
Wlkspd# = WalkSpd# / 2.0
else
Stfspd# = Strafespd#
Wlkspd# = WalkSpd#
endif
`MOVE CAMERA FORWARDS / BACK
if move <> 0
XTest# = Newxvalue(XTest#, CamAngY#, move*WlkSpd#)
ZTest# = Newzvalue(ZTest#, CamAngY#, move*WlkSpd#)
endif
`strafe camera
if strafe <> 0
XTest# = Newxvalue(XTest#, CamAngY#+90, strafe*StfSpd#)
ZTest# = Newzvalue(ZTest#, CamAngY#+90, strafe*StfSpd#)
endif
rem If we're within the world update with new position else forget it.
if (XTest# > 0) and (XTest# < WldLimX#) and (ZTest# > 0) and (ZTest# < WldLimZ#)
X# = XTest#
Z# = ZTest#
endif
GH# = get ground height(1,X#,Z#)
position camera X#,GH#+35,Z#
return
If you spotted it then I'm impressed because it is quite difficult to see.
The problem is that I'm messing around with the speed of the player at different angles and then combining the movements. I should have made the inputs adjust the
angle of movement instead.
[I'll just do that now and post...]
Hmm I'm actually stuck here!
It's harder than I thought.
Shh... you're pretty.