How can I make a more dynamic attack animation system for different towers. For example, some towers will shoot fireballs while some will dash towards the enemies or jump towards the enemyy? Similar to the way the towers attack in let’s say, Anime vanguards or anime adventuress…
Another thing is, I want the tower’s damage to have an area of effect of AOE
Here is the main Attack function inside the module that currently, every unit follows.
function UnitSpawnModule.Attack(newUnit, player)
local Config = newUnit:WaitForChild("Config")
local target = UnitSpawnModule.FindTarget(newUnit, Config.Range.Value, Config.TargetMode.Value)
if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
local targetcframe = CFrame.lookAt(newUnit.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newUnit.HumanoidRootPart.BodyGyro.CFrame = targetcframe
AnimateUnitEvent:FireAllClients(newUnit, "Attack1", target)
target.Humanoid:TakeDamage(Config.Damage.Value)
if target.Humanoid.Health <= 0 then
player.Money.Value += target.Money.Value
end
task.wait(Config.Cooldown.Value)
end
task.wait(.1)
if newUnit and newUnit.Parent then
UnitSpawnModule.Attack(newUnit, player)
end
end
Here is the local script that plays the animation:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Storage = game:GetService("ServerStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local AnimateUnitEvent = events:WaitForChild("AnimateUnit")
local function SetAnimation(Object, animName)
local humanoid = Object:WaitForChild("Humanoid")
local animationsFolder = Object:WaitForChild("Animations")
if humanoid and animationsFolder then
local animationObject = animationsFolder:WaitForChild(animName)
if animationObject then
local animator = humanoid:FindFirstChild("Animator") or Instance.new("Animator")
local playingTracks = animator:GetPlayingAnimationTracks()
for i, track in pairs(playingTracks) do
if track.Name == animName then
return track
end
end
local animationTrack = animator:LoadAnimation(animationObject)
return animationTrack
end
end
end
local function playAnimation(Object, animName)
local animationTrack = SetAnimation(Object, animName)
if animationTrack then
animationTrack:Play()
else
warn("ANIMATION DOES NOT EXIST")
return
end
end
AnimateUnitEvent.OnClientEvent:Connect(function(unit, animName)
playAnimation(unit, animName)
end)