跳至正文
版本:bleeding-edge 🩸

人工智能

AI 在 nanos world 中如何运作

每个在没有被 Player 占有的情况下生成的 Character 或 CharacterSimple,都将自动被一个 AI 控制器占有。这意味着你可以使用诸如 :MoveTo() 和 :LookAt() 等角色方法。

在 nanos world 中,AI 以及物理效果都是通过客户端进行分发计算并与其他玩家共享的(参考网络主控),这意味着 AI 只有在有玩家连接到服务器时才会工作。

信息

If you call :MoveTo() on a NPC and there is no Player connected, it will only start walking once a Player joins the server.

Moving NPCs​

The main AI methods are available in all Pawns (Character and CharacterSimple):

When a movement finishes (or fails, e.g. if there is no path), the event MoveComplete is triggered:

Server/Index.lua
-- Spawns a NPC (a Character not possessed by any Player)
local npc = Character(Vector(0, 0, 100), Rotator(), "nanos-world::SK_Male")

-- Makes it walk to a location, considering arrived when it's at 50 units from it
npc:MoveTo(Vector(1000, 0, 100), 50)

npc:Subscribe("MoveComplete", function(self, succeeded)
if (succeeded) then
Console.Log("The NPC arrived!")
else
Console.Log("The NPC couldn't find a path!")
end
end)

To find points where NPCs can walk to, the client side Navigation Static Class provides methods such as Navigation.GetRandomReachablePointInRadius() and Navigation.FindPathToLocation().

In order to NPCs to figure out the path to walk, the map will need to have a NavMesh configured.