Take a look at this code example.
// Project: Character Controller Demo
// Created: 2015-11-25
// set window properties
SetWindowTitle( "Character Controller Demo" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
Create3DPhysicsWorld()
global ground=200
global player=1
global rotationInc as float
rotationInc = 1.0
global finalRotation as float
init()
setcameraposition(1,getobjectx(player),getobjecty(player)+56,getobjectz(player)-180)
setcameralookat(1,getobjectx(player),getobjecty(player),getobjectz(player),0)
do
CharacterControllerInput(player,75.0)
Step3DPhysicsWorld()
//Position Camera after we step physics
setcameraposition(1,getobjectx(player),getobjecty(player)+56,getobjectz(player)-180)
setcameralookat(1,getobjectx(player),getobjecty(player),getobjectz(player),0)
Sync()
loop
function init()
setclearcolor(0,0,255)
make_my_object(ground,0,0,0,200,30,200,0,255,0,0)
make_my_object(player,0,30,0,16,17,15,255,0,0,1)
endfunction
function make_my_object(objid,x#,y#,z#,sx#,sy#,sz#,r,g,b,p)
createobjectbox(objid,sx#,sy#,sz#)
setobjectposition(objid,x#,y#,z#)
setobjectcolor(objid,r,g,b,255)
if p=0
createobjectphysics(objid,2,1)
endif
if p=1
`createobjectphysics(objid,2,1)
createmycontroller(objid)
endif
endfunction
function createobjectphysics(myobj,myphyshape,myphybody)
select myphybody
case 1
Create3DPhysicsStaticBody(myobj)
endcase
case 2
Create3DPhysicsDynamicBody(myobj)
endcase
endselect
select myphyshape
case 1
SetObjectShapesphere(myobj)
endcase
case 2
SetObjectShapebox(myobj)
endcase
endselect
endfunction
function createmycontroller(objid)
characterOffsetVec = CreateVector3( 0.0, 0.0, 0.0 )
//This vector indicates the axis your model faces forward.
//For example if your model faces down the - Z axis use this vector.
objectOrientationVec = CreateVector3( 0.0, 0.0, 0.0 )
//The crouchScale parameter is 0.0 to 1.0, for examle 0.75 is 75% of the models standing height.
Create3DPhysicsCharacterController(objid, 1, characterOffsetVec, objectOrientationVec, 0.75 )
DeleteVector3( characterOffsetVec )
DeleteVector3( objectOrientationVec )
Set3DPhysicsCharacterControllerPosition(objid,getobjectx(player),getobjecty(player),getobjectz(player))
endfunction
function CharacterControllerInput( objID as integer, walkVelocity as float)
if GetKeyboardExists()
//Stops movement when no key is pressed.
Move3DPhysicsCharacterController( objID, 0, WalkVelocity)
//W Key Forwards
if GetRawKeyState( 87 )
Move3DPhysicsCharacterController( objID, 1, WalkVelocity )
endif
//S key Backwards
if GetRawKeyState( 83 )
Move3DPhysicsCharacterController( objID, 2, WalkVelocity )
endif
//A Key Strafe Left
if GetRawKeyState( 65 )
Move3DPhysicsCharacterController( objID, 3, WalkVelocity )
endif
//D Key Strafe Right
if GetRawKeyState( 68 )
Move3DPhysicsCharacterController( objID, 4, WalkVelocity )
endif
//SpaceBar Jump
if GetRawKeyPressed( 32 )
Jump3DPhysicsCharacterController( objID )
endif
//Q Key For Rotate
if GetRawKeyState( 81 )
finalRotation = finalRotation - rotationInc
endif
//E Key For Rotate
if GetRawKeyState( 69 )
finalRotation = finalRotation + rotationInc
endif
Rotate3DPhysicsCharacterController( objID, finalRotation )
endif
endfunction
The coffee is lovely dark and deep,and I have code to write before I sleep.