We use cookies on this site to enhance your user experience

Top Down Action: SummonerBot

Top Down Action: SummonerBot

5 min

The next enemy robot that we’ll implement are the summoner robots. These robots do not attack the player directly, but instead will summon PiBots if they see the player nearby. Fortunately, we can reuse a lot of the code we used with the PiBots since the underlying structure of the SummonerBots won’t be much different.

TDS_SummonerBot.png

The first thing we need to do is edit the SummonerBot script that is contained in the RobotScripts folder inside of ServerStorage. Like the PiBot script, this script gets copied automatically into all of the SummonerBots in the game. Let’s start off with just the basic code of the bot:

local AIController = require(game.ServerStorage.RobotScripts.RobotController)

local robot = script.Parent

local configurations = {}
configurations.AggroDistance = 30
configurations.ActionDistance = 30
configurations.ActionCooldown = 4
configurations.LastAction = 0

configurations.Action = function(target)
	
end

configurations.Aggro = function(target)
	
end

AIController:RunAI(robot, configurations)

Just like the PiBot Script, we have to setup a configurations table that contains the variables and functions the RobotController ModuleScript will use to run our AI.

Now let’s add the code to summon a PiBot on the Summoner’s action:

local AIController = require(game.ServerStorage.RobotScripts.RobotController)

local robot = script.Parent
local maxSummonDistance = 10

local configurations = {}
configurations.AggroDistance = 30
configurations.ActionDistance = 30
configurations.ActionCooldown = 4
configurations.LastAction = 0

configurations.Action = function(target)
	local toTarget = target.Torso.Position - robot.Torso.Position
	local offsetDistance = math.min(maxSummonDistance, toTarget.magnitude/2)
	local summonOffset = toTarget.unit * offsetDistance
	local summon = game.ServerStorage.Robots.PiBot:Clone()
	summon:SetPrimaryPartCFrame(robot.Torso.CFrame + summonOffset)
	summon.Parent = game.Workspace.Robots
end

configurations.Aggro = function(target)
	
end

AIController:RunAI(robot, configurations)

We start off by getting the vector that starts at the Summoner’s torso and ends at its target’s torso. We then calculate an offset distance. We don’t want this distance to be more than 10 studs (which we store in maxSummonDistance to make sure the summoner isn’t putting a robot on the other side of the map. If the player is closer than 10 studs though, we want to put the summon in between the player and the Summoner. We use math.min to pick the smaller of the two values given to it, in this case either 10 or half the distance between the player and Summoner. We can then multiply this minimum value by the vector we calculated earlier to get an actual offset in studs to place the new robot.

Next we can create a new robot by using the Instance/Clone|Clone function on the stored PiBot in the Robots Folder inside of ServerStorage. We can position this new robot with the Model/SetPrimaryPartCFrame|SetPrimaryPartCFrame function. We pass the summonOffset we calculated earlier added to the summoner’s torso’s CFrame to this function, which will place the summoned robot in the correct position. Lastly, we set the Parent of the summoned robot to game.Workspace.Robots. This will put the robot in the Workspace so that it shows in the 3D view, and will also trigger the Instance/ChildAdded|ChildAdded event in our AIScriptGiver Script so the new robot gets its AI scripts.

Animations

Let’s add effects and an animation to the summon action. The summoner bots already have an Animation we can use.

local AIController = require(game.ServerStorage.RobotScripts.RobotController)

local robot = script.Parent
local maxSummonDistance = 10
local summonAnimationTrack = robot.Humanoid:LoadAnimation(robot.SummonAnimation)
local summonActivationTime = 4

local configurations = {}
configurations.AggroDistance = 30
configurations.ActionDistance = 30
configurations.ActionCooldown = 4
configurations.LastAction = 0

configurations.Action = function(target)
	summonAnimationTrack:Play()
	wait(summonActivationTime)
	if robot.Humanoid.Health > 0 then
		local toTarget = target.Torso.Position - robot.Torso.Position
		local offsetDistance = math.min(maxSummonDistance, toTarget.magnitude/2)
		local summonOffset = toTarget.unit * offsetDistance
		local summon = game.ServerStorage.Robots.PiBot:Clone()
		summon:SetPrimaryPartCFrame(robot.Torso.CFrame + summonOffset)
		summon.Parent = game.Workspace.Robots
	end
end

configurations.Aggro = function(target)
	
end

AIController:RunAI(robot, configurations)

Like all other animations we’ve used so far, we first load the Animation into an AnimationTrack using the Humanoid Humanoid/LoadAnimation|LoadAnimation function. We also setup a variable for the duration of the animation. Then, in the Action function, we can play the AnimationTrack. We then want to include a wait so that the animation can finish playing before we summon the new robot. We also then check to make sure the SummonerBot hasn’t been knocked out during the animation by looking at the Humanoid/Health|Health property of its Humanoid.

The SummonerBots also have ParticleEmitters built into their hands, which we can activate to make a neat looking spell effect. All we have to do is enable these emitters before the animation plays and disable them when the animation is finished.

local AIController = require(game.ServerStorage.RobotScripts.RobotController)

local robot = script.Parent
local maxSummonDistance = 10
local summonAnimationTrack = robot.Humanoid:LoadAnimation(robot.SummonAnimation)
local summonActivationTime = 4

local configurations = {}
configurations.AggroDistance = 30
configurations.ActionDistance = 30
configurations.ActionCooldown = 4
configurations.LastAction = 0

configurations.Action = function(target)
	robot['Right Hand'].ParticleEmitter.Enabled = true
	robot['Left Hand'].ParticleEmitter.Enabled = true
	summonAnimationTrack:Play()
	wait(summonActivationTime)
	robot['Right Hand'].ParticleEmitter.Enabled = false
	robot['Left Hand'].ParticleEmitter.Enabled = false
	if robot.Humanoid.Health > 0 then
		local toTarget = target.Torso.Position - robot.Torso.Position
		local offsetDistance = math.min(maxSummonDistance, toTarget.magnitude/2)
		local summonOffset = toTarget.unit * offsetDistance
		local summon = game.ServerStorage.Robots.PiBot:Clone()
		summon:SetPrimaryPartCFrame(robot.Torso.CFrame + summonOffset)
		summon.Parent = game.Workspace.Robots
	end
end

configurations.Aggro = function(target)
	
end

AIController:RunAI(robot, configurations)

TDS_SummonerAnimation.png