I’ve read up a little on this topic, but I couldn’t really find a sensible answer to my question.
I’m making a wave defense game, and I’m currently in the works of making this enemy shoot a rocket launcher towards an enemy it sees. My current progress is that is does spawn the rocket, and the rocket does move, just not in any normal direction.
My goal is to have the rocket glide through seamlessly through the air and towards the enemy, or your player model.
Here’s my code:
local NPC = script.Parent
local debounce = false
local damage = 50
local attackRate = 3
local rocketSpeed = 50
local health = 250
NPC.Humanoid.MaxHealth = health
NPC.Humanoid.Health = health
function FindPlayer(Position)
local List = game.Workspace:GetChildren()
local Torso = nil
local Distance = 250
local HumanoidRootPart = nil
local Humanoid = nil
local Player = nil
for i = 1, #List do
Player = List[i]
if (Player.ClassName == "Model") and (Player ~= script.Parent) and (game.Players:GetPlayerFromCharacter(Player)) then
HumanoidRootPart = Player:FindFirstChild("HumanoidRootPart")
Humanoid = Player:FindFirstChild("Humanoid")
if (HumanoidRootPart ~= nil) and (Humanoid ~= nil) and (Humanoid.Health > 0) then
if (HumanoidRootPart.Position - Position).Magnitude < Distance then
Torso = HumanoidRootPart
Distance = (HumanoidRootPart.Position - Position).Magnitude
end
end
end
end
return Torso
end
task.spawn(function()
while task.wait(3) do
local target = FindPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
local assets = game:GetService('ServerStorage').Assets
local rocket = assets.Rocket
local newRocket = rocket:Clone()
newRocket.CollisionGroup = 'Rocket'
newRocket.Velocity = rocket.CFrame.LookVector * rocketSpeed
local antiGravity = Instance.new('BodyForce', newRocket)
antiGravity.Force = Vector3.new(0, newRocket:GetMass() * workspace.Gravity, 0)
newRocket.CFrame = CFrame.lookAt(NPC.RocketLauncher.Handle.Position, target.Position)
newRocket.Parent = workspace
newRocket:SetNetworkOwner(nil)
game:GetService('Debris'):AddItem(newRocket, 30)
end
end
end)
while task.wait(0.25) do
local Target = FindPlayer(script.Parent.HumanoidRootPart.Position)
if Target ~= nil then
script.Parent.Humanoid:MoveTo(Target.Position, Target)
end
end
(Rocket code is near the bottom)
I’ve been stuck on this for longer than I want to be, and would greatly appreciate help!
Make a folder in ServerStorage called ‘Assets’, then make a MeshPart called ‘Rocket’ adn give it the MeshID: http://www.roblox.com/asset/?id=2251534 and the size 1.2, 1.2, 3.27.
Also add collision groups ‘RocketLauncher’ and ‘Rocket’. Make it so Rocket cannot collide with Rocket and RocketLauncher.
local NPC = script.Parent
local debounce = false
local damage = 50
local attackRate = 3
local rocketSpeed = .5
local health = 250
NPC.Humanoid.MaxHealth = health
NPC.Humanoid.Health = health
function FindPlayer(Position)
local List = game.Workspace:GetChildren()
local Torso = nil
local Distance = 250
local HumanoidRootPart = nil
local Humanoid = nil
local Player = nil
for i = 1, #List do
Player = List[i]
if (Player.ClassName == "Model") and (Player ~= script.Parent) and (game.Players:GetPlayerFromCharacter(Player)) then
HumanoidRootPart = Player:FindFirstChild("HumanoidRootPart")
Humanoid = Player:FindFirstChild("Humanoid")
if (HumanoidRootPart ~= nil) and (Humanoid ~= nil) and (Humanoid.Health > 0) then
if (HumanoidRootPart.Position - Position).Magnitude < Distance then
Torso = HumanoidRootPart
Distance = (HumanoidRootPart.Position - Position).Magnitude
end
end
end
end
return Torso
end
task.spawn(function()
while task.wait(3) do
local target = FindPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil and NPC.Humanoid.Health > 0 then
local assets = game:GetService('ServerStorage')
local rocket = assets.rocket
local newRocket = rocket:Clone()
newRocket.CollisionGroup = 'Rocket'
local antiGravity = Instance.new('BodyForce', newRocket)
antiGravity.Force = Vector3.new(0, newRocket:GetMass() * workspace.Gravity, 0)
newRocket.CFrame = CFrame.lookAt(NPC.RocketLauncher.Handle.Position, target.Position)
local Direction = (target.Position - NPC.RocketLauncher.Handle.Position) -- Gets the directional vector
newRocket.Velocity = Direction * rocketSpeed -- Shoots it in that direction
newRocket.Parent = workspace
newRocket:SetNetworkOwner(nil)
game:GetService('Debris'):AddItem(newRocket, 30)
end
end
end)
while task.wait(0.25) do
local Target = FindPlayer(script.Parent.HumanoidRootPart.Position)
if Target ~= nil then
script.Parent.Humanoid:MoveTo(Target.Position, Target)
end
end
What happened was the way it was shot, I used the direction formula (To - From) to find a vector to which the projectile will be shot at, I put the speed at .5 for it could be visible to me
Your method is essentially the same as HP’s. Considering yours kind of slows down depending on the distance between the elf and the player, the rocket doesn’t work as well when the player is closer.
Both methods appear to have the rocket spiral out of control at some distance.
It looks like, for your method in this scenario,
local Direction = (target.Position - NPC.RocketLauncher.Handle.Position) -- Gets the directional vector
newRocket.Velocity = Direction * rocketSpeed -- Shoots it in that direction
determined the rocket speed, which could probably cause issues the closer the enemy is to the elf.
yeah Im not sure how to do it the “physics” way but I believe the more traditional method would be best, as vectors have more a magnitude and a direction
I was able to fix this problem by using HP’s method and configuring the elf model so that all of its body parts had a collision group that the rocket could not interact with. Thanks for all the help, though!