I'm currently not really using a dummy for the cam object.
Function UpdatePlayer()
// store players original x,y,z and angles before any movement..
oldPlayerX#=GetObjectX(PlayerObjectID)
oldPlayerY#=GetObjectY(PlayerObjectID)
oldPlayerZ#=GetObjectZ(PlayerObjectID)
// object_hit_infront=ObjectRayCast(0,oldPlayerX#,oldPlayerY#,oldPlayerZ#,oldPlayerX#,oldPlayerY#,oldPlayerZ#+5)
//If object_hit_infront<> 0 // we've hit something then adjust the x,y,z's ..
//zdist#=GetObjectRayCastDistance( 0 )
//endif
// -------------------------- jumping.. ------------------------------------------------------------------
If GetRawKeyState(KEY_SPACE) and falling=0 // Space = jump
If jump=0 and Timer()>jumpwait# // inair=0 // if not juming/inair already, so no jumping when falling..
jump=1 // intitiate new jump
jumpwait#=Timer()+.4 // delay should so player cannot initiate jumping right after for is weird,
Endif
Endif
// excute jump..
if jump=1 // if jump has been initiated
upy#=upy#+40 //(gravity#*jmultiplier#) // count up till max jump heigth reached.
if upy#>=maxjump# // if maxheight reached then reset y to 0 and no end jump..
upy#=0
jump=0
else // if not reached maximum height yet then..
MoveObjectLocalY(PlayerObjectID,40) // the actual jump.
endif
endif
// -------------------------------- if this bit put at camera section, then no smooth movement --------------
// get input from mouse pointer..(to turn player left/right and/or turn cam up/dow)
PointerX#=GetPointerX()
PointerY#=GetPointerY()
If mouseLocked=0
setrawmouseposition(GetVirtualWidth()*0.5,GetVirtualHeight()*0.5) // re-center mouse on screen
EndIf
GetPointerMoveX#=(PointerX#-GetPointerX()) // calculate distance moved
GetPointerMoveY#=(PointerY#-GetPointerY())
// for turning left/right..
If GetRawKeyState(KEY_LEFT) OR GetRawKeyState(KEY_A) // Left/A = turn player left
GetPointerMoveX# = -hspeed#
Endif
If GetRawKeyState(KEY_RIGHT) OR GetRawKeyState(KEY_D) // Right/D = turn player right
GetPointerMoveX# = hspeed#
Endif
// get current camera angles..
CameraAngleX#=CameraAngleX#+GetPointerMoveY#*FrameTime#
CameraAngleY#=CameraAngleY#+GetPointerMoveX#*FrameTime# // calc camera angle
UserCameraDistance#=UserCameraDistance#-GetRawMouseWheelDelta()*40 // zoom in/out when used mousewheel
if UserCameraDistance#>MaxCameraDistance# then UserCameraDistance#=MaxCameraDistance# //limit max dist from cam
if UserCameraDistance#<MinCameraDistance# then UserCameraDistance#=MinCameraDistance# // limit minimum dist from cam
// --------------------------------------------------------------------------------------------------------
// For moving forward/backward..
// get player's AGK joystick input.
// On PC WASD are used, on devices without kybd will create a virtual onscreen joystick.
// ( can optionally be positiioned differently on screen using SetJoystickScreenPosition ).
joystick_x# = GetJoyStickX()*hspeed# // horizontal movement input
joystick_y# = GetJoyStickY()*-vspeed# // vertical movement input
// get vertical keybpard input (PC only)..
speed#=1 //
If (GetRawKeyState(KEY_UP) = 1 or GetRawKeyState(KEY_W) or GetRawMouseRightState() ) AND GetRawKeyState(KEY_SHIFT) // Up/W/RBM + SHIFT = move player forward(run)
speed#=3 // Shift = speedup.
joystick_y#=1*vspeed# *speed# //addjust z
ElseIf GetRawKeyState(KEY_UP) = 1 or GetRawKeyState(KEY_W) or GetRawMouseRightState() // Up/W/RBM = move player forward (walk)
speed#=1 // without Shift = normal speed.
joystick_y#=1*vspeed# *speed# //addjust z
Else
endif
If GetRawKeyState(KEY_DOWN) = 1 // Down/S = move player backward
joystick_y#=1*-vspeed# *speed#
Endif
// move player based on the input of joystick and mouse..
RotateObjectLocalY(PlayerObjectID,joystick_x#) // seems redundant,but somehow makes cam movement more fluid?
MoveObjectLocalZ(PlayerObjectID,joystick_y#)
SetObjectRotation(PlayerObjectID,0,CameraAngleY#,0) // set player at Y angle calculated according to mousemovement, don't move X angle for player will then tilt up/down
// for strafe.. ( if not put here then doesn''t work )
If GetRawKeyState(KEY_Q) // Q = move (strafe) player left
joystick_y# = .1*-hspeed#
MoveObjectLocalX(PlayerObjectID,joystick_y#)
Endif
If GetRawKeyState(KEY_E) // E = move (strafe) player right
joystick_y# = .1*hspeed#
MoveObjectLocalX(PlayerObjectID,joystick_y#)
Endif
// -------------------------- apply gravity.. --------------------------------------------------------------
if flymode=1
falling=0 // gravity#=0.0
else // always apply gravity, unless fly mode active..
falling=1 //gravity#=0.5
endif
if jump=0
MoveObjectLocalY(PlayerObjectID,-gravity#) ///2) // gravity ""always on'' except when jumping..
endif
// --------------------------------------------------------------------------------------------------------
// ################### manage collision checking for the player with other geometry #############
// get players new x,y,z's for usage with collisions..
newPlayerX#=GetObjectX(PlayerObjectID)
newPlayerY#=GetObjectY(PlayerObjectID)
newPlayerZ#=GetObjectZ(PlayerObjectID)
// sphere cast between the old and new positions of the player to see if we hit something while moving to new location..
object_hit = ObjectSphereSlide(0,oldPlayerX#,oldPlayerY#,oldPlayerZ#,newPlayerX#,newPlayerY#,newPlayerZ#,playerwidth#)
// if the sphere has collided with an object then calculate sphere new position to give sliding collision
If object_hit <> 0 // we've hit something then adjust the x,y,z's ..
newestPlayerX#=GetObjectRayCastSlideX(0)
if newestPlayerX#<>newPlayerX#
hitobjectatx=1
endif
newestPlayerY#=GetObjectRayCastSlideY(0)
if newestPlayerY#<>newPlayerY#
hitobjectaty=1
falling=0
endif
newestPlayerZ#=GetObjectRayCastSlideZ(0)
if newestPlayerZ#<>newPlayerZ#
hitobjectatz=1
endif
//climbing=0
//if (newestPlayerZ#<>newPlayerZ# or newestPlayerX#<>newPlayerX#) and newestPlayerY#>newPlayerY# then climbing=1
if object_hit = ladderhitbox or object_hit = ladderhitbox2 //or object_hit= Stairs
gravity#= -70 //-.5 //1 // reduce/reverse gravity
elseif object_hit = pad
jump=1
maxjump#=600
gravity#=-30 //1 // cancel out gravity
else // if hit something else..
gravity#= 20 //70 //.5
maxjump#=200 // 5 //*physscale
endif
else // nothing hit
hitobjectatx=0 : hitobjectaty=0 : hitobjectatz=0
//climbing=0
newestPlayerX#=newPlayerX#
newestPlayerY#=newPlayerY#
newestPlayerZ#=newPlayerZ#
gravity#= 20 //60 //.8 // 2 // .5
EndIf
// prevent falling through floor due to gravity update..
If newestPlayerY# <=playerheight#/2 //1 // so if gravity aplied but now ending up below the btm floor.. (could also be checked against terrainy.
newestPlayerY#=playerheight#/2 //1 // limit/reset y to minimm y so don't fall through floor
falling=0
endif
// (re)position onto new location..
SetObjectPosition(PlayerObjectID, newestPlayerX#, newestPlayerY#, newestPlayerZ# ) //process all modifications made above.
// **************************************************************************************
// ************************************ CAMERA MOVEMENT **********************************
// **************************************************************************************
// #################### manage camera avoiding geometry inbetween player and cam ################
// get players new x,y,z's for usage with cam..
PlayerX#=GetObjectX(PlayerObjectID)
PlayerY#=GetObjectY(PlayerObjectID) +40 // set to above player
PlayerZ#=GetObjectZ(PlayerObjectID)
// ----------------------------------------------------------------------------------------------------------------
If cammode=0 // 3rd person mode(default)..
SetCameraPosition(1,PlayerX#,PlayerY#,PlayerZ#) // place camera at new player position, but a bit higher(shoulderheight.
//SetCameraRotation(1,GetObjectAngleX(PlayerObjectID), GetObjectAngleY(PlayerObjectID) ,GetObjectAngleZ(PlayerObjectID) ) //
//---- limit cam x angle otherwise may tilt too far up or down..
// limit angle up.. (should be higher so can look all the way up to e.g. building/mountain).
if CameraAngleX#<= -50
CameraAngleX#=-50
endif
// limit cam down..
If CameraAngleX#>100 //90
CameraAngleX#=100 //90
endif
// initially move camera as set above by inputs of controllers..
MoveCameraLocalZ(1,-MaxCameraDistance#) // move cam behind player
//gun..
SetobjectRotation(rightarmID,CameraAngleX#,0,0) // adjust gun position according to cam
// ----------------------------------------------------------------------------------------------------------------
// obstacle avoiding cam..(reacts to above cam move)
RayObjectID=ObjectRayCast(0,PlayerX#,PlayerY#,PlayerZ#,GetCameraX(1),GetCameraY(1),GetCameraZ(1)) // sent a ray from players pos to cam behind player.
if RayObjectID<>0 // if an object was hit, this means there's an object between player and cam, obstructing the view'
NewCameraDistance#=GetObjectRayCastDistance(0) // get distance to where has been hit.
else
NewCameraDistance#=UserCameraDistance# // nothing's hit so just set to user defined zoom distance.
endif
SetCameraPosition(1,PlayerX#,PlayerY#,PlayerZ#) // set initial cam position at player.
// handle rotation..
CameraDistance#=CurveValue(NewCameraDistance#,CameraDistance#,10) // calc new camdistance (Destination#,Current#,Steps#)
if CameraDistance#<MinCameraDistance# then CameraDistance#=MinCameraDistance#
If CameraAngleX# < -10 // so if cam angle negative , we're looking up..
MoveCameraLocalY(1,-CameraAngleX#*1.05) // move cam to new position above the player when needed, so floor don't get in the way
else
MoveCameraLocalY(1,CameraAngleX#*1.05) // move cam to new position above the player when needed, so floor don't get in the way
endif
SetCameraRotation(1,CameraAngleX#,CameraAngleY#,0) // set camera at angle calculated according to mousemovent
MoveCameraLocalZ(1,-CameraDistance#/2) // move cam to new distance position (behind/above the player. regardless of orientation)
// ----------------------------------------------------------------------------------------------------------------
ElseIf cammode =1 // 1st person mode..
SetCameraPosition(1,PlayerX#,PlayerY#+24,PlayerZ#-16) // place camera at player position
SetCameraRotation(1,CameraAngleX#,CameraAngleY#,0) // set camera at angle calculated according to mousemovent
SetObjectRotation(rightarmID,CameraAngleX#,0,0) // adjust gun position according to cam
// need to hide face objects?
//SetObjectVisible(PlayerObjectID,cammode)
SetCameraPosition(1,PlayerX#,PlayerY#+(.6*physscale),PlayerZ#-(.4*physscale)) // place camera at player position
// ----------------------------------------------------------------------------------------------------------------
Elseif cammode =2 // detached cam mode..(currently only for debugging) // still needs cam correction for not allowed angles.as in 3rd pers mode.
//Setcameralookat(1,PlayerX#,PlayerY#+(.6*physscale),PlayerZ#-(.4*physscale),0) // follow the player around from afar
Setcameralookat(1,PlayerX#,20,PlayerZ#-16,0) // follow the player around from afar
//CameraDistance#=CurveValue(NewCameraDistance#,CameraDistance#,10)
if CameraDistance#<MinCameraDistance#*4
CameraDistance#=MinCameraDistance#*4
SetCameraPosition(1,PlayerX#,PlayerY#,PlayerZ#) // set initial player position.
If jump=0
// MoveCameraLocaly(1,-adjustfactorY#) ///5) // move cam to new position above the player when needed, so floor don't get in the way
MoveCameraLocalZ(1,-CameraDistance#) // move cam to new position behind the player.
endif
endif
// need to still make cam move along when player moves sideways or wanders off to far depth wise.. obstacle avoiding can also be used here,
SetobjectRotation(rightarmID,CameraAngleX#,0,0) // adjust gun position according to cam(mouse actually in this case)
Endif
// ----------------------------------------------------------------------------------------------------------------
EndFunction
But I'm sure that would make it even better, for gives bit more control.
You could create a box object with CreateObjectBox(), set it invisible with SetObjectVisible() and turn of it's collision with SetObjectCollisionMode() otherwise it would still be messing with things, then use FixObjectToObject() as Markus said.