Hello! How can I make an automatic turret? Automatic meaning it automatically aims towards the player and fires bullets in the direction of the player…? And how could I make those bullets damage the player…
Really useful tutorial by the DevKing -
(SIDE NOTE: This video is a bit outdated. The ray casting used in this video is deprecated which means it won’t work. However you can learn how to use the new raycasting system and implement it into this tutorial.)
Hopefully this helps
yeah but it stays in one position. I want mine to always shoot towards the player
It does do exactly that, but he also says use the new ray casting system
If I’m not wrong, the tutorial shows how to replicate that. The way I would program this is by manipulating the turrets CFrame which can be used to face the player.
what you need to make something face another are just 2 positions.
you guessed it, the target and the turret.
for the code to work like a turret, (always searching for target to hit), you need to put it in a while (condition) loop.
firstly, you need to specify your target, be it a children of an instance (example: game.Players and getting the character for each player)
then, you need to specify the range of your turret
then, you make the turet take action when the range condition is met.
pseudocode:
local function GetClosestCharacter ( fromPosition, range)
local bestMagnitude = 1000000
local closestChar = nil
for i, v in ipairs(game.Players:GetChildren()) do
local char = v.Character
local head = char.Head
local distance = (fromPosition - head.Position).Magnitude
if distance < bestMagnitude and distance < range then
bestMagnitude = distance
closestChar = char
end
end
return closestChar
end
then, you put the function in a while loop, combined with other operators to make the turret do an action when a character is found.
local cooldown = 1
local range = 20
while true do
local closestChar = GetClosestCharacter (turret.Position, range)
if closestChar then
AttackTarget(closestChar)
end
wait(cooldown)
end
the AttackTarget function will just error the code, i’m letting you fill in what AttackTarget(closestChar) does by making a function of the same name above the loop.
- To make the turret face the target, use CFrame.lookAt( vector3 position, vector3 target)
- To make the humanoid take damage, use humanoid:TakeDamage( number )
- To make the projectile visual, you can either use TweenService, or you can just make a part that stretches from the turret to the target (You can also use physics)
the only part where raycast is necessary is when you want to check if there are any obstacle between you and the target
Alright so… I have the system where it looks for the player, but instead of damaging the player within a range, I want it actually to projectile towards the player. Basically giving the player a chance to dodge the the projectile.
In that case, you want to use physics instead. If your projectile is slow enough, use a bullet that uses .Touched event + body velocity to move
if your bullet moves fast like, a rocket from a helicopter then you don’t want to use .Touched, but instead you can raycast the difference of position every .Heartbeat to check if the bullet hits anything
so i’ll go with the bullet with body velocity
firstly you make the bullet part with a bodyvelocity for the script to use
(just make it in studio and clone it later)
so now you have the bullet, go to your AttackTarget function
and replace the code that uses :TakeDamage() and replace it with a launch projectile function instead
in my code i will make the projectile shoot forward from a part named “barrel”
local function LaunchProjectile()
local bulletSpeed = 20 -- dont set this too high or .touched becomes bad
local bulletPrefab = workspace.Bullet -- This depends on your bullet location
local barrel = script.Parent.Barrel -- assuming that is where your barrel is
local bullet = bulletPrefab:Clone()
local targetHit = false -- to avoid target taking damage a lot of times
bullet.CFrame = barrel.CFrame
bullet.BodyVelocity.Velocity = barrel.CFrame:PointToObjectSpace(Vector3.new(0,0, -1)) * bulletSpeed
-- some people use lookVector but i dont
bullet.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil and targetHit == false then
humanoid:TakeDamage( number )
targetHit = true
end
end)
end
Ahh. Alright so, lets say I have the turret constantly facing the player… Will this system always fire the bullet where the Turret is facing?
Use bodygyro to make it move
BG.CFrame = CFrame.new(Turret.Position,EnemyRoot.Position)
pretty sure its sum like dat
yes it will but if the bullet goes weird the PointToObjectSpace is probably not correct
also make sure the bullet’s Front face is where you want the bullet to face when its flying
++ same for the barrel
wait i forgot to parent it lol
do bullet.Parent = workspace
and make sure to destroy it when it hits the target
after
targetHit = true
do
bullet:Destroy()
Alright, Ill tell you if I run to any issues. Thanks!
Alright so, it always faces one direction instead of firing from the turret
Code:
local function LaunchProjectile()
local bulletSpeed = 3 -- dont set this too high or .touched becomes bad
local bulletPrefab = workspace.Bullet -- This depends on your bullet location
local barrel = script.Parent -- assuming that is where your barrel is
local bullet = bulletPrefab:Clone()
local targetHit = false -- to avoid target taking damage a lot of times
bullet.Parent = workspace
bullet.CFrame = barrel.CFrame
bullet.BodyVelocity.Velocity = barrel.CFrame:PointToWorldSpace(Vector3.new(0,0, -1)) * bulletSpeed
-- some people use lookVector but i dont
print("HEY")
bullet.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil and targetHit == false then
humanoid:TakeDamage(5)
targetHit = true
end
end)
end
while wait(0.5) do
LaunchProjectile()
end
the projectile isnt anchored and cancollide is off. And a bodyVelocity is inside of the projectile
it should fire where your barrel part is facing, combine it with the turret facing system you have
(might wanna give me screenshot if you think i dont get what you mean)
Oh wait, I just figured out… The direction does in fact change in the bodyVelocity… But it doesnt fire away from the face of the turret.
https://gyazo.com/56f0ecc977cc76d57e0155dd8516f326
Notice how it does change its velocity direction?
is the black hole supposed to be the barrel?
hmm try this
bodyVelocity.Velocity = (barrel.CFrame:PointToWorldSpace(Vector3.new(0,0, -1)) - barrel.Position) * bulletSpeed