The command
set camera to follow combined with sparky's is the best way to go. This code is off the top of my head and untested (I can't compile at work), but it should point you in the right direction:
rem setup screen
sync on
sync rate 60
backdrop on
color backdrop 0
hide mouse
rem make a level object
make object plain 1 , 0 , 0
make object box 2 , 100 , 1 , 100
make mesh from object 1 , 2
delete object 1
make object cylinder 2 , 30
make mesh from object 2 , 3
delete object 2
add limb 1 , 1 , 1
add limb 1 , 2 , 2 : offset limb 1 , 2 , 30 , 15 , 0
add limb 1 , 3 , 2 : offset limb 1 , 3 , 0 , 15 , 30
delete mesh 1
delete mesh 2
rem make texture for level object
create bitmap 1 , 32 , 32
a2box 0 , 0 , 31 , 31 , 0xFF00FF00
a2box 8 , 8 , 23 , 23 , 0xFF400000
get image 1 , 0 , 0 , 32 , 32
delete bitmap 1
texture object 1 , 1
scale object texture 1 , 128 , 128
rem setup level for collision
SC_SetupComplexObject 1 , 1 , 2
rem make player object
make object sphere 2 , 10
rem starting positions
x# = 0
y# = 5
z# = 0
gravity# = 0
rem main loop
do
rem user info
center text 512 , 10 , "Use WASD and space"
rem control game elements
gosub _Control_Player
gosub _Control_Camera
rem refresh screen
sync
rem end of main loop
loop
rem end
end
rem subroutines
rem -------------------------------------------
_Control_Player:
rem old positions
oldx# = x#
oldy# = y#
oldz# = z#
rem calculate input angle
a = -1
if keystate(17) then a = 0
if keystate(30) then a = 270
if keystate(31) then a = 180
if keystate(32) then a = 90
if keystate(17) and keystate(30) then a = 315
if keystate(30) and keystate(31) then a = 225
if keystate(31) and keystate(32) then a = 135
if keystate(32) and keystate(17) then a = 45
rem jump
if spacekey() and gravity# = 0 then gravity# = 1
rem calculate player angle and speed
if a > -1
angle# = curveangle(a + camera angle y() , angle# , 5)
speed# = curvevalue(2 , speed# , 5)
else
speed# = curvevalue(0 , speed# , 5)
endif
rem calculate new positions
x# = newxvalue(x# , angle# , speed#)
z# = newzvalue(z# , angle# , speed#)
rem gravity
dec gravity# , 0.01
inc y# , gravity#
rem collision with level
if SC_SphereCast(1 , oldx# , oldy# , oldz# , x# , y# , z# , 5 , 0)
x# = SC_GetCollisionSlideX()
y# = SC_GetCollisionSlideY()
z# = SC_GetCollisionSlideZ()
rem reset gravity if ground is beneath player
if SC_RayCast(1 , x# , y#-4 , z# , x# , y#-6 , z# , 0)
gravity# = 0
endif
endif
rem update player
position object 2 , x# , y# , z#
yrotate object 2 , angle#
return
_Control_Camera:
rem control camera
set camera to follow x# , y# , z# , angle# , 40 , 15 , 3 , 0
point camera x# , y# , z#
rem store old positions of camera
oldcamx# = camx#
oldcamy# = camy#
oldcamz# = camz#
rem get positions of camera
camx# = camera position x()
camy# = camera position y()
camz# = camera position z()
rem check for collision
if SC_SphereCast(oldcamx# , oldcamy# , oldcamz# , camx# , camy# , camz# , 2 , 0)
camx# = SC_GetCollisionSlideX()
camy# = SC_GetCollisionSlideY()
camz# = SC_GetCollisionSlideZ()
endif
rem update camera
position camera camx# , camy# , camz#
point camera x# , y# , z#
return
TheComet