Game Development Reference
In-Depth Information
Create the Lua file as follows:
src/my_sandbox/script/FollowerAgent.lua
FollowerAgent.lua :
require "AgentUtilities";
local leader;
function Agent_Initialize(agent)
AgentUtilities_CreateAgentRepresentation(
agent, agent:GetHeight(), agent:GetRadius());
-- Randomly assign a position to the agent.
agent:SetPosition(Vector.new(
math.random(-50, 50), 0, math.random(-50, 50)));
-- Assign the first valid agent as the leader to follow.
local agents = Sandbox.GetAgents(agent:GetSandbox());
for index = 1, #agents do
if (agents[index] ~= agent) then
leader = agents[index];
break;
end
end
end
function Agent_Update(agent, deltaTimeInMillis)
local deltaTimeInSeconds = deltaTimeInMillis / 1000;
local sandboxAgents =
Sandbox.GetAgents(agent:GetSandbox());
-- Calculate a combining force so long as the leader
stays
-- within a 100 meter range from the agent, and has
less than
-- 180 degree difference in forward direction.
local forceToCombine =
Search WWH ::




Custom Search