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.NavFollowerOptionssightInterval(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 resultpath(Array.<BGE.NavPoint>) — The path being followed, in the grid's coordinates, and the waypoint it's heading forpathIndex(integer)lastSearch(dynamic) — The last search to finish (a BGE.NavPathSearch), and how many steer() calls it took;lastSearchFrames(integer)
Example
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 ifConstructor
new NavFollower(
nav: BGE.NavGrid,
options?: BGE.NavFollowerOptions,
): NavFollowerParameters
nav(BGE.NavGrid) — the grid to path acrossoptions(BGE.NavFollowerOptions, optional, default: "{}")
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 positiontarget(BGE.Math.Vector) — where it's goingdt(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
Parameters
from(BGE.Math.Vector)target(BGE.Math.Vector)
Returns
boolean
planeY(position: BGE.Math.Vector): float
Parameters
position(BGE.Math.Vector)
Returns
float