How to make a part follow player using body velocity?

I am trying to make a heat seeking missile but i only know how to use bodyPosition to make something follow you. But there is some problems with bodyPosition like when the part reaches near you it starts to have the same speed as the player.

Code for bodyPosition follow script :

bin = script.Parent

function move(target)
	local dir = (target.Position - bin.Position).unit
	local spawnPos = bin.Position
	local pos = spawnPos + (dir * 1)
	bin:findFirstChild("BodyGyro").cframe = CFrame.new(pos,  pos + dir)
end

function moveTo(target)
	bin.BodyPosition.position = target.Position
end

function findNearestTorso(pos)
	local list = game.Workspace:GetChildren()
	local torso = nil
	local dist = 1000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("Head")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end



while true do
	local torso = findNearestTorso(bin.Position)
	if torso~=nil then
		move(torso)
		moveTo(torso)
	end
	wait()
end

I’ve recently worked with RocketPropulsion, so I decided to do a bit of experimenting and came up with this. RocketPropulsion makes it easy to detect when the target has been reached, and BodyGyro prevents the rocket from strangely spinning while flying. I also made it so the rocket is destroyed if it hasn’t hit anything within the specified time.


SCRIPT:

local Propulsion = script.Parent.RocketPropulsion
local ActiveTime = tick() + 10 --Time the rocket has to reach its target. Change the 10 to add more time.
local IsActive = false

Propulsion.ReachedTarget:Connect(function()
	--What happens when the target has been reached.
end)

function FindNearest()
	local Dist = 1000 --Missile's range. If players are further than this, they can't be hit.
	local Closest = nil
	
	for i,v in pairs(game.Workspace:GetChildren()) do
		local Head = v:FindFirstChild("Head")
		local Hum = v:FindFirstChild("Humanoid")
		
		if Hum and Head and Hum.Health > 0 and v ~= script.Parent then --Checks if target has a head and is alive.
			local Mag = (Head.Position-script.Parent.Position).Magnitude --Checks distance from target.
			
			if Mag < Dist then --Checks if target is in range.
				Dist = Mag
				Closest = Head
			end
		end
	end
	
	return Closest --Returns the closest target's head.
end

while true do
	local ClosestTarg = FindNearest()
	
	if ClosestTarg ~= nil then
		local dir = (ClosestTarg.Position - script.Parent.Position).Unit
		local pos = script.Parent.Position + dir
		
		Propulsion.Target = ClosestTarg --Changes the RocketPropulsion's Target.
		script.Parent.BodyGyro.CFrame = CFrame.new(pos,pos + dir) --Changes the BodyGyro's CFrame.
		
		if IsActive == false then
			IsActive = true
			script.Parent.BodyGyro.MaxTorque = Vector3.new(400000,0,400000)
			Propulsion:Fire() --Activates the rocket.
		end
		
		--What happens when the rocket changes its target.
	else
		if IsActive == true then
			IsActive = false
			script.Parent.BodyGyro.MaxTorque = Vector3.new(0,0,0)
			Propulsion:Abort() --Stops the rocket.
		end
		
		Propulsion.Target = nil
		
		--What happens if there's nobody in the rocket's range.
	end
	
	if tick() > ActiveTime then
		--What happens if the rocket has failed to reach a target within the specified time.
		script.Parent:Destroy()
		break
	end
	wait() --How often the rocket will check for the nearest target.
end

You never explained what “RocketPropulsion” is.

If you are interested, please find all the documentation here. The basic idea is that it applies a force so that the object moves towards a certain point and also faces that point at the same time (just like a real rocket would). There is also the .ReachedTarget event which allows you to know when the target has been reached.

My reply is very old and I noticed that RocketPropulsion was deprecated some time after I made the post. Just keep that in mind in case you are planning on using it. It might be good to consider using LineForce, AlignPosition, AlignOrientation or something else.

1 Like