EntityWalkSpeed# ( entity )

Parameters

entity - the entity to ask about

Description

Returns how fast a walker is moving now, in metres a second.

The speed is the walker's own, across the ground. It rises as the walker sets off, at the acceleration WalkSpeed gives it, holds at its walking or running speed, dips as it steps round others, and falls to 0 as it slows down at its goal, as the example's graph shows. While the walker crosses a link, such as a jump or a ladder, it is the walker's walking speed. A push (see WalkPushable) isn't counted. An entity that isn't a NavWalker returns 0.

Use it for whatever should keep pace with the walker: footsteps and dust that come faster when it runs, the wheels of a cart, or animations of your own once WalkAnimations entity,False has left them to the program. To tell walking from running, compare it with the speeds you gave WalkSpeed: a walker's animation set changes from walk to run above halfway between them.

It changes at UpdateWorld, so read it after.

See also: WalkSpeed, WalkAnimations, WalkState, WalkEntity.

Example

; EntityWalkSpeed Example
; -----------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

; A raised camera looking down on the courtyard
camera=CreateCamera()
PositionEntity camera,0,13,-12
RotateEntity camera,50,0,0

light=CreateLight()
RotateEntity light,45,45,0

; A 20 x 20 m courtyard, one unit to a metre: the floor's top is at y=0
floor=Block(0,-0.5,0,20,0.5,20)
EntityColor floor,60,80,55

; Walls round the edge, and two inner walls with a gap at one end
Block 0,0,-10,20,1.5,0.5
Block 0,0,10,20,1.5,0.5
Block -10,0,0,0.5,1.5,20
Block 10,0,0,0.5,1.5,20
Block -2,0,-3,16,1.5,0.5
Block 2,0,3,16,1.5,0.5

; A guard who paces the near side, pausing a second at each end
guard=Character(-7,-6.5,220,60,60)
goal_x#=7
run=True
WalkEntity guard,goal_x,0,-6.5,run
pause=0

; The speeds of the last 10 seconds, for the graph
Dim speeds#(299)
frame=0

While Not KeyDown(1)

    ; R switches between walking and running at once
    If KeyHit(19) Then
        run=Not run
        WalkEntity guard,goal_x,0,-6.5,run
    EndIf

    ; At one end, it waits a second, then sets off for the other
    If WalkState(guard)=WALK_ARRIVED Then
        pause=pause+1
        If pause>60 Then
            pause=0
            goal_x=-goal_x
            WalkEntity guard,goal_x,0,-6.5,run
        EndIf
    EndIf

    ; Arrow keys move the camera (turning round the world's up axis)
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0,True
    If KeyDown(205) Then TurnEntity camera,0,-1,0,True

    UpdateWorld

    ; How fast the guard is going now, in metres a second: every second
    ; frame, the graph moves on
    speed#=EntityWalkSpeed(guard)
    frame=frame+1
    If frame Mod 2=0 Then
        For i=0 To 298
            speeds(i)=speeds(i+1)
        Next
        speeds(299)=speed
    EndIf

    RenderWorld

    ; The graph: 0 at the bottom, 4 m/s at the top
    Color 40,40,40
    Rect 170,380,300,90
    Color 255,220,0
    For i=1 To 299
        Line 170+i-1,470-speeds(i-1)*22,170+i,470-speeds(i)*22
    Next
    Color 255,255,255
    pace$="walking"
    If run Then pace="running"
    Text 0,0,"R: walk / run   Arrow keys: move camera   Esc: exit"
    Text 0,20,"EntityWalkSpeed(guard)="+Int(speed*10)/10.0+" m/s ("+pace+")"
    Text 170,362,"The last 10 seconds, 0 to 4 m/s"

    Flip

Wend

End

; A stone block standing on y, centred on x,z
Function Block(x#,y#,z#,width#,height#,depth#)
    b=CreateCube()
    ScaleEntity b,width/2,height/2,depth/2
    PositionEntity b,x,y+height/2,z
    EntityColor b,125,110,90
    Return b
End Function

; A character 0.6 m across and 1.8 m tall, with a visor on its face, kept out
; of the walkable map it walks on
Function Character(x#,z#,red,green,blue)
    c=CreateCube()
    ScaleMesh c,0.3,0.9,0.3
    PositionEntity c,x,0.9,z
    EntityColor c,red,green,blue
    visor=CreateCube(c)
    ScaleMesh visor,0.2,0.08,0.06
    PositionEntity visor,0,0.6,0.32
    EntityColor visor,30,30,40
    Return c
End Function

Index