Well Ive been away from DBP for a few months because my comp was broken and I was to lazy to do anything about it. Well I got it fixed and as a warm up I decided to do this. Turned out I forgot that I wasnt great before so I ended up learning a good chunk of this while I wrote it. Its 248 lines of code for controlling a character in 3rd person with the mouse to turn. It includes:
Mouse controlled turning
Forward/backward + sidesteping using W,A,S,D keys.
Fully functional Gravity
Jumping
3 different type of movent(Standing,crouching,prone/on stomach)with correct speed and turning plus different sized objects.
Almost everything is in its own function except jumping and changing position, but is well organised,
rem's identifying everything.
Hope this will help some people out, have fun with it. The code is posted below. If you find any errors bugs, or if somthing is unclear just post and I will try to help out.
EDIT: sorry in the functions I did some bad spacing it doesnt change code, just a little harder to look at, nothing major ill post it in a code thing here:
`Created by Nick Purbrick AKA GOD. I would like it if I recieve credit if some or all
`of my work is used, though it isn't manditory. I hope this will help SOMEONE out.
`Happy Coding
`setup system info
setup()
`make objects
make_stuff()
do
`jump code (FUNTION NEEDED)
x#=object position x(1)
y#=object position y(1)
z#=object position z(1)
matrix#=get ground height(1,x#,z#)
ch=y#+height#
if spacekey()=1 and jumpb=0
height#=height#+10
jumpb=1
endif
if jumpb=1
position object 1, x#,y#+height#,z#
endif
if ch>matrix#
height#=height#-.5
endif
if ch<matrix#
jumpb=0
position object 1,x#,(matrix+object size y(1)),z#
height#=0
endif
`get movment method
if movment=0 then movment=1
if keystate(2)=1 then movment=1 :`standing
if keystate(3)=1 then movment=2 :`crouching
if keystate(4)=1 then movment=3 :`prone/on stomach
`update different movment methods
if movment=1 then movment_standing(1)
if movment=2 then movment_crouching(1)
if movment=3 then movment_prone(1)
`update size of object accourding to stance
if movment=1 then scale object 1,100,100,100
if movment=2 then scale object 1,100,60,100
if movment=3 then scale object 1,100,20,300
`update the camera
chase_cam(1)
`gravity
gravity(1,1,1)
` print fps
fps()
`sync
nsync()
loop
`gravity
function gravity(Pull,object,matrix)
if object position y(object)>get ground height(matrix,object position x(object),object position z(object))+object size y(object)
move object down object,Pull
endif
endfunction
`make sphere and matrix
function make_stuff()
make object sphere 1,5
position object 1,1,3,1
make matrix 1,1000,1000,100,100
endfunction
`sync and mouse position and all that fun stuff
function setup()
sync on
sync rate 60
`hide mouse
position mouse 520,400
endfunction
`chase cam
function chase_cam(id)
`work out the angle of the object being chased
yAng#=wrapvalue(object angle y(id)+180)
`grab the objects current position
xPos#=object position x(id)
yPos#=object position y(id)
zPos#=object position z(id)
`other variables
camDist=15
camHeight=3
`work out new position
xCamPos#=newxvalue(xPos#,yAng#,camDist)
zCamPos#=newzvalue(zPos#,yAng#,camDist)
`work out camera height
yCamPos#=get ground height(1,xCamPos#,zCamPos#)+camHeight
if yCamPos# < yPos#+camHeight then yCamPos#=yPos#+camHeight
`update camera position
position camera xCamPos#,yCamPos#,zCamPos#
point camera xPos#,yPos#+camHeight,zPos#
endfunction
`movment code(moving,side-steping,turning) for standing up
function movment_standing(id)
s#=screen width()
yAng#=object angle y(id)
x#=object position x(id)
y#=object position y(id)
z#=object position z(id)
If keystate(17)=1
XTest# = Newxvalue(X#,yAng#,.5)
ZTest# = Newzvalue(Z#,yAng#,.5)
X#=XTest#
Z#=ZTest#
Endif
If keystate(31)=1
XTest# = Newxvalue(X#,Wrapvalue(yAng#-180),.5)
ZTest# = Newzvalue(Z#,Wrapvalue(yAng#-180),.5)
X#=XTest#
Z#=ZTest#
Endif
If keystate(30)=1
XTest# = Newxvalue(X#,yAng#-90,.5)
ZTest# = Newzvalue(Z#,yAng#-90,.5)
X#=XTest#
Z#=ZTest#
Endif
If keystate(32)=1
XTest# = Newxvalue(X#,yAng#+90,.5)
ZTest# = Newzvalue(Z#,yAng#+90,.5)
X#=XTest#
Z#=ZTest#
Endif
if mousex()<s#*.44 then yrotate object id,(yang#-.3)
if mousex()<s#*.40 then yrotate object id,(yang#-1)
if mousex()<s#*.35 then yrotate object id,(yang#-2)
if mousex()<s#*.1 then yrotate object id,(yang#-4)
if mousex()>s#*.56 then yrotate object id,(yang#+.3)
if mousex()>s#*.60 then yrotate object id,(yang#+1)
if mousex()>s#*.65 then yrotate object id,(yang#+2)
if mousex()>s#*.9 then yrotate object id,(yang#+4)
position object 1,x#,y#,z#
endfunction
`movment code(moving,side-steping,turning) for crouching
function movment_crouching(id)
s#=screen width()
yAng#=object angle y(id)
x#=object position x(id)
y#=object position y(id)
z#=object position z(id)
If keystate(17)=1
XTest# = Newxvalue(X#,yAng#,.2)
ZTest# = Newzvalue(Z#,yAng#,.2)
X#=XTest#
Z#=ZTest#
Endif
If keystate(31)=1
XTest# = Newxvalue(X#,Wrapvalue(yAng#-180),.1)
ZTest# = Newzvalue(Z#,Wrapvalue(yAng#-180),.1)
X#=XTest#
Z#=ZTest#
Endif
If keystate(30)=1
XTest# = Newxvalue(X#,yAng#-90,.1)
ZTest# = Newzvalue(Z#,yAng#-90,.1)
X#=XTest#
Z#=ZTest#
Endif
If keystate(32)=1
XTest# = Newxvalue(X#,yAng#+90,.1)
ZTest# = Newzvalue(Z#,yAng#+90,.1)
X#=XTest#
Z#=ZTest#
Endif
if mousex()<s#*.42 then yrotate object id,(yang#-.4)
if mousex()<s#*.35 then yrotate object id,(yang#-.5)
if mousex()<s#*.1 then yrotate object id,(yang#-1)
if mousex()>s#*.58 then yrotate object id,(yang#+.4)
if mousex()>s#*.65 then yrotate object id,(yang#+.5)
if mousex()>s#*.9 then yrotate object id,(yang#+1)
position object 1,x#,y#,z#
endfunction
`movment code(moving,side-steping,turning) for prone/on stomach
function movment_prone(id)
s#=screen width()
yAng#=object angle y(id)
x#=object position x(id)
y#=object position y(id)
z#=object position z(id)
If keystate(17)=1
XTest# = Newxvalue(X#,yAng#,.08)
ZTest# = Newzvalue(Z#,yAng#,.08)
X#=XTest#
Z#=ZTest#
Endif
If keystate(31)=1
XTest# = Newxvalue(X#,Wrapvalue(yAng#-180),.04)
ZTest# = Newzvalue(Z#,Wrapvalue(yAng#-180),.04)
X#=XTest#
Z#=ZTest#
Endif
If keystate(30)=1
XTest# = Newxvalue(X#,yAng#-90,.06)
ZTest# = Newzvalue(Z#,yAng#-90,.06)
X#=XTest#
Z#=ZTest#
Endif
If keystate(32)=1
XTest# = Newxvalue(X#,yAng#+90,.06)
ZTest# = Newzvalue(Z#,yAng#+90,.06)
X#=XTest#
Z#=ZTest#
Endif
if mousex()<s#*.42 then yrotate object id,(yang#-.2)
if mousex()<s#*.35 then yrotate object id,(yang#-.2)
if mousex()<s#*.1 then yrotate object id,(yang#-.3)
if mousex()>s#*.58 then yrotate object id,(yang#+.2)
if mousex()>s#*.65 then yrotate object id,(yang#+.2)
if mousex()>s#*.9 then yrotate object id,(yang#+.3)
position object 1,x#,y#,z#
endfunction
`print screen fps
function fps()
print screen fps()
endfunction
`sync program and refresh the screen
function nsync()
sync
endfunction