How to make Tower Defense bullets

I’m trying to make a tower defense game but I can’t seem to figure out how to make the bullet trails that you see in other tower defense games. I do have a system that hits the nearest mob to it but I can’t seem to figure out how I can make bullets go to the mob the tower is targeting.

2 Likes

Make tower shoot pew pew at target position with trail attached to pew pew.

local bullet = bullet:Clone() --A small part that can be invisble if you want
bullet.Position = tower.Position
bullet.LinearVelocity = tower.Position * tower.CFrame.LookVector * 90 -- number is speed of bullet

.LookVector is also considering the tower is looking at the enemy, if not you can just make the bullet look at the enemy by

local lookTo = Vector3.new(Enemy.HumanoidRootPart.Position.X,Enemy.HumanoidRootPart.Position.Y,Enemy.HumanoidRootPart.Position.Z)
Bullet.CFrame = CFrame.new(Bullet.Position, LookTo)
Bullet.LinearVelocity = Bullet.Position * Bullet.CFrame.LookVector * 90 -- number is speed of bullet

If this isn’t what you are looking for, please reply with more information.

1 Like

So this is my script right now

function find_nearest_target(tower_new, range)
	local nearest_target = nil

	for i, target in pairs(workspace.AlphaMap.mobholder:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - tower_new.HumanoidRootPart.Position).Magnitude
		if distance < range then
			nearest_target = target
			range = distance
		end
	end
	return nearest_target
end

function tower.Attack(tower_new, player)
	local config = tower_new.Stats
	local target = find_nearest_target(tower_new, config.range.Value) 
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		local targetCFrame = CFrame.lookAt(tower_new.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		tower_new.HumanoidRootPart.BodyGyro.CFrame = targetCFrame 
		tower_animate:FireAllClients(tower_new, "Attack")
		target.Humanoid:TakeDamage(config.damage.Value)
		task.wait(config.time.Value)
	end
	task.wait(0.1)
	tower.Attack(tower_new, player)
end

and so I tried the .LookVector but it just gave me a stream or errors.

If possible can you take a look at the code and tell me where I need to implement the bullet script?

You add the bullet script before target.Humanoid:TakeDamage()

Basically what you are doing is making a bullet with a trail shoot from the tower to the target, this will all just be visual, this is how other tower defense might do their effects, for example, a shooting bullet like what you want

and LookVector shouldn’t give a stream of errors but looking at your script it would look like this

local bullet = bullet:Clone() --A small part that can be invisble if you want
bullet.Position =  tower_new.HumanoidRootPart.Position
bullet.Parent = workspace
local bv = instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = tower_new.HumanoidRootPart.CFrame.LookVector * 90 -- number is speed of bullet
bv.Parent = bullet

So I tried your script but this happened…

3 Likes

will the same go for an “animated” FX like for example a Goku blast?

1 Like

@StraightScared Quick question but the bullet isnt going the way the tower is facing and also how do I make the bullet disappear when it hits the target?

1 Like