Skip to content

Navigation and AI

SgNavMesh objects represent walkable surfaces for pathfinding. The nav mesh is built from scene geometry; scripts query paths and nearest points through the SgNavMesh API.

Setup

  1. Place walkable geometry in the scene (floors, terrain).
  2. Add an SgNavMesh object and bake/configure it in the editor.
  3. Query paths from a behaviour script on an agent.

See SgNavMesh for field reference and the engine-api block generated from Lua comments.

Typical agent loop

local SgNavMesh = require 'engine/sceneobjects/sgnavmesh'
local Vector3 = require 'engine/math/vector3'

-- navMesh reference via Property.Node or findByName
Agent:addProperty("navMesh", Property.Node)

function Agent:update(deltaTimeMs)
    if not self.path or #self.path == 0 then return end

    local target = self.path[self.pathIndex]
    local pos = self.node.transform.position
    local dir = target:sub(pos):normalized()
    local dt = deltaTimeMs / 1000

    self.node:translate(dir.x * self.speed * dt, 0, dir.z * self.speed * dt)

    if Vector3.distance(pos, target) < 0.5 then
        self.pathIndex = self.pathIndex + 1
    end
end

function Agent:moveTo(destination)
    -- Request path from nav mesh API (see SgNavMesh reference)
    -- self.path = ...
    -- self.pathIndex = 1
end

Use Physics Scripting raycasts for line-of-sight checks alongside nav paths.

Vehicles

Wheeled vehicles use SgVehicle instead of navmesh steering. See SgVehicle.

Further reading