Making an NPC fire a rocket

Hey, developers!

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.

Here’s a little video to help vizualise my current situation:
https://gyazo.com/e804326234f2b4ab2d974c81e953ad8a

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!

2 Likes

Could you provide the NPC with the rocket and the scripts for me to visualize it a bit more

Sure. Here’s the asset link to the NPC model:

1 Like

Assets is not a valid member of ServerStorage “ServerStorage”

Oops!

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.

Here is what I did:

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 = CFrame.lookAt(NPC.RocketLauncher.Handle.Position,target.Position).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)

I simply copied what newRocket.CFrame had into newRocket.Velocity, lol. Does this work? It works for me.

I was gonna do bodyforces but I was going to what was put on here lol

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

Solved

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

As much as this did fire towards the player, it occasionally spiraled out of control or overshot the player’s head.

Does my method work? it works with everything you’ve provided

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’m going to give raycasts a shot and see how that works

c;.;.;./‘’‘’‘’‘’‘’‘’‘’‘’/'/oy\\\\\

Don’t worry, it happened with all the iterations. It might be a problem with the way the rocket is set up when it fires.

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!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.