Try doing a linepick downwards, a short distance. If the picked distance is greater than the player collision sphere, he's not touching the ground, so apply gravity to him :)
This is a linepick example.
USE arrow keys to move, and spacebar to jump. Line pick used to see if the cone is still 'jumping'
PositionEntity cube5,-4,11,12
EntityPickMode cube5,2
EntityType cube5,2
Global cube6=CreateCube()
PositionEntity cube6,-6,13,10
EntityPickMode cube6,2
EntityType cube6,2
Global cube7=CreateCube()
PositionEntity cube7,0,13,20
ScaleEntity cube7,4,0.2,4
EntityPickMode cube7,2
EntityType cube7,2
Global sphere=CreateSphere(10)
ScaleEntity sphere,10,1,10
EntityPickMode sphere,2
EntityType sphere,2
PositionEntity sphere,-2,0,19
Collisions 1,2,2,2; set up collisions between the ground and the player
Global jump_force#
Global jump_flag=0; set jump flag to 'no jump'
Global gravity#=0.9; set gravity value
While Not KeyHit(1)
LinePick(EntityX(player,True),EntityY(player,True),EntityZ(player,True),0,-30,0); do a linepick straight down form the players location
If jump_flag=0 Then
If EntityY#(player,True)-PickedY()>1.05 Then; WARNING. THIS NUMBER MUST BE SLIGHTLY LARGER THAN THE PLAYERS ENTITYRADIUS.
; ABOVE. if the players y position is greater than a distance of 1.05, then make the player fall.
; Since the players entity radius is 1, This number must be slightly greater than 1, so that the collision
; system doesn't work.
jump_flag=1; set jump flag to jump
jump_force=0.02; set jump force to 0.02. You cannot set this to 0. It must always be slightly more
End If
End If
If KeyHit(57) And jump_flag=0 Then; if user presses spacebar
jump_flag=1; set jump flag to 'jump'
jump_force=0.4; set jumping force. make larger for higher jump
End If
If KeyDown(200) Then MoveEntity player,0,0,0.1
If KeyDown(208) Then MoveEntity player,0,0,-0.1
If KeyDown(203) Then TurnEntity player,0,1,0
If KeyDown(205) Then TurnEntity player,0,-1,0
If jump_flag=1 Then; if jump flag set then 'jump'
update_jump()
End If
update_camera()
UpdateWorld
RenderWorld
Text 0,0," Press Space to jump. Arrow keys to move!"
;DebugLog(" pickedy="+PickedY())
Flip
Wend
End
Function update_camera()
RotateEntity camera,EntityPitch(player),EntityYaw(player),EntityRoll(player); make the camera equal to the rotations of the camera
PositionEntity camera,EntityX(player),EntityY(player)+2,EntityZ(player); position the camera at the x and z pos of the player
MoveEntity camera,0,0,-7; move the camera back from the player by 7 units
PointEntity camera,player
End Function
Function update_jump()
jump_force=jump_force*gravity; apply gravity to the jumping force
MoveEntity player,0,jump_force,0; move the player
If jump_force>0 And jump_force<0.05 Then; when players upward speed reaches alomost zero, reverse it
; make smaller to keep player in the air for longer
jump_force=jump_force*-1; reverse the jumping force
gravity=gravity+(2*(1-gravity)); turn gravity around
End If
If jump_force<-0.5 Then jump_force=-0.5; stop the player from falling to quick
If EntityCollided(player,2) Then; if the player has collided with the ground
If EntityY(player,True)-PickedY()<=1 Then; WARNING. THIS NUMBER MUST BE THE SAME AS THE PLAYER'S ENTITYRADIUS
jump_force=0; reset the jumping force
jump_flag=0; reset jump flag back to zero (not jumping)
gravity=gravity-(2*(gravity-1)); reset gravity
End If
End If
End Function
|