NavFollower

Steers an agent to a target round the walls of a BGE.NavGrid. While the agent can see the target, steer() returns invalid and the game moves it however it likes (straight at it, circling, attacking). Otherwise it follows a path, searched again on a timer.

Sight is checked a few times a second, not every frame. Override checkSight() for another test (e.g. a raycast, or a view cone).

Properties

  • nav (BGE.NavGrid)
  • plane (BGE.SeparationPlane) — Options: see BGE.NavFollowerOptions
  • sightInterval (float)
  • repathInterval (float)
  • repathDistance (float)
  • minRepathInterval (float)
  • arriveRadius (float)
  • searchBudgetMs (integer)
  • searchOptions (BGE.NavSearchOptions)
  • pathWhileVisible (boolean)
  • sightSnapRings (integer)
  • canSeeTarget (boolean) — The last line-of-sight result
  • path (Array.<BGE.NavPoint>) — The path being followed, in the grid's coordinates, and the waypoint it's heading for
  • pathIndex (integer)
  • lastSearch (dynamic) — The last search to finish (a BGE.NavPathSearch), and how many steer() calls it took;
  • lastSearchFrames (integer)

Example

CODE
m.follower = new BGE.NavFollower(nav, {plane: BGE.SeparationPlane.xz, arriveRadius: 0.5})
' each frame:
way = m.follower.steer(m.position, player.position, dt)
if invalid <> way
m.moveBy(way, m.speed * dt)
else if m.follower.canSeeTarget
m.moveToward(player.position, dt)
end if

Constructor

new NavFollower( nav: BGE.NavGrid, options?: BGE.NavFollowerOptions, ): NavFollower

Parameters


Instance Methods

steer( from: BGE.Math.Vector, target: BGE.Math.Vector, dt: float, ): dynamic

Which way to go: a unit vector on the plane along the path, or invalid when the agent can see the target (check canSeeTarget) or has no path yet or left (e.g. the target is walled off and it's as close as it can get).

Parameters

  • from (BGE.Math.Vector) — the agent's position
  • target (BGE.Math.Vector) — where it's going
  • dt (float) — seconds since the last call

Returns

  • dynamic — a BGE.Math.Vector, or invalid

canSee( from: BGE.Math.Vector, target: BGE.Math.Vector, dt: float, ): boolean

Whether the agent can see the target, checked every sightInterval seconds. steer() calls it; call it yourself for sight without steering (e.g. to wake a guard). Calls share one timer, so call it once per frame.

Parameters

  • from (BGE.Math.Vector)
  • target (BGE.Math.Vector)
  • dt (float) — seconds since the last call

Returns

  • boolean

forceRepath(): void

Searches again on the next steer() and checks sight again, e.g. after a door opened. The current path is kept until the new one is found.

Returns

  • void

repathAfter(seconds: float): void

Waits this long before the next search, e.g. to spread several agents' first searches over different frames.

Parameters

  • seconds (float)

Returns

  • void

clearPath(): void

Drops the path (and any running search); the next steer() out of sight searches at once.

Returns

  • void

checkSight( from: BGE.Math.Vector, target: BGE.Math.Vector, ): boolean

protected

Parameters

  • from (BGE.Math.Vector)
  • target (BGE.Math.Vector)

Returns

  • boolean

planeY(position: BGE.Math.Vector): float

protected

Parameters

  • position (BGE.Math.Vector)

Returns

  • float