I was going through some code in one of my folders and found an example I typed up earlier this year in answer to a question. I thought I'd post it here. It's a way of toggling between 3rd person and 1st person perspective. In 1st person mode, you can also look around or aim with just the 'head' of the character.
The character, in this case, is a small green cube stacked on a white cube. The green cube is the head.
Press the [SPACE] key to toggle between perspectives. While in 1st person, hold the RIGHT mouse button and a circle target reticle will appear on the screen. Move the mouse around and the green head will move and you can aim. The body will stay still.
In both modes, move with the arrow keys and steer with the mouse. You can move around freely in either mode but while holding the Right Mouse button and aiming, you cannot move.
rem limb look around
rem by latch
rem 2/22/2009
set display mode 800,600,32
sync on
sync rate 60
autocam off
hide mouse
rem make an object with a head
make object cube 1,25
make object cube 2,10
make mesh from object 2,2
add limb 1,1,2
delete object 2
delete mesh 2
offset limb 1,1,0,20,0
color limb 1,1,rgb(0,255,0)
position object 1,5000,13,5000
set object rotation zyx 1
rem make a matrix to walk around on
make matrix 1,10000,10000,30,30
randomize matrix 1,100
do
gosub _move_object
sync
loop
`----------------------------------------------------------
_move_object:
rem get the current object x and z position
objx#=object position x(1)
objz#=object position z(1)
rem get rotation by mouse movement
yang#=wrapvalue(object angle y(1)+mousemovex())
yrotate object 1,yang#
rem use the mouse rotation angle, the current object position
rem and move it 10 units forward or backward
newx#=newxvalue(objx#,yang#,((upkey()-downkey())*10))
newz#=newzvalue(objz#,yang#,((upkey()-downkey())*10))
position object 1,newx#,objy#,newz#
rem get the y height of the ground for the object
objy#=get ground height(1,newx#,newz#)+13
rem position the camera
rem check the toggle state of the spacekey (mode)
os=ns
ns=spacekey()
if os > ns
mode=1-mode
endif
rem based on the mode, set the camera height, distance, and smoothness
rem of the camera follow.
if mode=0
`3rd person
camdist=150
camheight=60+objy#
smooth=5
endif
if mode=1
`1st person
camdist=1
camheight=objy#+20
endif
rem set the camera to follow the object
set camera to follow newx#,objy#,newz#,yang#,camdist,camheight,smooth,1
camx#=camera angle x()
camy#=camera angle y()
rem now enter look around mode
if mode=1
smooth=1
rem if the right click is held - look around
while mouseclick()=2
rem target
ink rgb(255,0,0),0
circle screen width()/2,screen height()/2,20
rem figure out head rotations
headyang#=wrapvalue(limb angle y(1,1)+mousemovex())
headxang#=wrapvalue(limb angle x(1,1)+mousemovey())
rem rotate the head
rotate limb 1,1,headxang#,headyang#,0
cy#=limb direction y(1,1)
cx#=limb direction x(1,1)
rotate camera cx#,cy#,0
sync
endwhile
rem restore original rotations
rotate camera camx#,camy#,camera angle z()
rotate limb 1,1,0,0,0
endif
return
Enjoy your day.