I am working on an epic game. There will be AI fighter planes protecting ships and shooting down enemy planes on both sides. I want to make it so the planes attack japanese aircraft and if there are none, just orbit around it’s aircraft carrier. This is my patrol AI code.
local model = script.Parent.Parent
local targets = Instance.new("Folder")
targets.Parent = script.Parent
local function getTargets()
for v,t in pairs(workspace:GetDescendants()) do
local enemyPlane = t:FindFirstAncestor("JapFighter")
local enemyBomber = t:FindFirstAncestor("JapBomber")
local enemyHeavy = t:FindFirstAncestor("JapHeavy")
if enemyPlane or enemyBomber or enemyHeavy then
v.Parent = targets
end
end
end
local function patrol():
end
local function FireGuns()
for v,Part in pairs(script.Parent:GetDescendants()) do
if Part.Name == "MachineGun" then
local bullet = Instance.new("Part")
bullet.Position = Part.Position
bullet.CanCollide = false
local v = Instance.new("BodyVelocity")
bullet.Orientation = Part.Orientation
bullet.Name = "AB"
v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
v.Velocity = bullet.CFrame.LookVector * -500
v.Parent = bullet
bullet.Parent = workspace
bullet.Size = Vector3.new(1,1,1)
bullet.Material = "Neon"
bullet.BrickColor = BrickColor.new("Really blue")
end
end
end
Right now, I already have a decent shooting script and my plane already moves forward in a seperate script. I want to make it so when targets is empty, the plane orbits above workspace.uscv. I want the plane to fire machine guns when a target is infront. If there is a target, adjust orientation in it’s direction. Please help.
Also, How do I make a propeller spin?
local model = script.Parent
local targets = Instance.new("Folder")
targets.Name = "targets"
targets.Parent = script.Parent
local function getTargets()
for v,t in pairs(workspace:GetDescendants()) do
local enemyPlane = t:FindFirstAncestor("JapFighter")
local enemyBomber = t:FindFirstAncestor("JapBomber")
local enemyHeavy = t:FindFirstAncestor("JapHeavy")
if enemyPlane or enemyBomber or enemyHeavy or t.Name == "JapFighter" then
targets = t.Parent
end
end
end
local function FireGuns(target)
for v,Part in pairs(script.Parent:GetDescendants()) do
if Part.Name == "MachineGun" and Part.Position.X == target.Position.X or Part.Position.Z == target.Position.Z then
local bullet = Instance.new("Part")
bullet.Position = Part.Position
bullet.CanCollide = false
local v = Instance.new("BodyVelocity")
bullet.Orientation = Part.Orientation
bullet.Name = "AB"
v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
v.Velocity = bullet.CFrame.LookVector * -500
v.Parent = bullet
bullet.Parent = workspace
bullet.Size = Vector3.new(1,1,1)
bullet.Material = "Neon"
bullet.BrickColor = BrickColor.new("Really blue")
wait(0.25)
end
end
end
local function shootDownEnemyPlanes()
getTargets()
local partsTable = targets:GetChildren()
local closest = {nil,math.huge}
for i,v in pairs(partsTable) do
local distance = (script.Parent.hemalurgicSpike.Position - v.Position).magnitude
if distance <= closest[2] then
closest = {v, distance}
local target = closest
local direction = CFrame.new(script.Parent.hemalurgicSpike.CFrame.Position, target.CFrame.Position).lookVector
script.Parent.Engine.Direction = direction
FireGuns(target)
end
end
end
while true do
shootDownEnemyPlanes()
wait(0.01)
end
Right now, targets is empty
Now, it is not changing direction.
I don’t need it to fire guns rn. Targets works, and direction is found, I just need the plane to move in the direction of the direction.
local model = script.Parent
local e = Instance.new('BodyForce')
e.Parent = script.Parent.Engine
local closest = nil
local distance = 999999999999999
local targets = Instance.new("Folder")
targets.Name = "targets"
targets.Parent = workspace
local function getTargets()
for v,t in pairs(workspace:GetDescendants()) do
local enemyPlane = t:FindFirstAncestor("JapFighter")
local enemyBomber = t:FindFirstAncestor("JapBomber")
local enemyHeavy = t:FindFirstAncestor("JapHeavy")
if enemyPlane or enemyBomber or enemyHeavy or t.Name == "JapFighter" then
t.Parent = targets
end
end
end
local function FireGuns(closest)
for v,Part in pairs(script.Parent:GetDescendants()) do
if Part.Name == "MachineGun" then
if Part.Position.X == closest.Position.X or Part.Position.x == closest.Position.X then
local bullet = Instance.new("Part")
bullet.Position = Part.Position
bullet.CanCollide = false
local v = Instance.new("BodyVelocity")
bullet.Orientation = Part.Orientation
bullet.Name = "AB"
v.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
v.Velocity = bullet.CFrame.LookVector * -500
v.Parent = bullet
bullet.Parent = workspace
bullet.Size = Vector3.new(1,1,1)
bullet.Material = "Neon"
bullet.BrickColor = BrickColor.new("Really blue")
end
end
end
end
local function shoot(closest)
FireGuns(closest)
wait(0.25)
end
local function shootDownEnemyPlanes()
getTargets()
for i,v in pairs(targets:GetChildren()) do
local distance1 = (script.Parent.hemalurgicSpike.Position - v.Position).magnitude
if distance >= distance1 then
closest = v
distance = distance1
local direction = CFrame.new(script.Parent.Engine.CFrame.Position, closest.CFrame.Position).LookVector
e.Force = direction
end
end
end
while true do
shootDownEnemyPlanes()
wait(0.01)
end
Can someone like @Forummer pretty please help? I really need this please please please.