How can I make my helicopter NPC avoid flying into parts?

Right now my helicopter NPC works well, but only on a blank baseplate. It cannot be used anywhere else because it will just fly into walls. This is sort of what I’m trying to make it do

This is the function I use to find if there is a part heading in its direction (it works fine, could be better):

function FindInFront(Debug)
	if not FindInFrontDB then
		FindInFrontDB = true
		spawn(function()
			wait(0.15)
			FindInFrontDB = false
		end)
		local ok = P.SearchPart.CFrame.LookVector * 1000
		local ray = Ray.new(P.SearchPart.CFrame.p, (ok - P.SearchPart.CFrame.p).unit * 500)
		local part, position = workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList1, false, true)
		local beam = nil
		if Debug then
			beam = Instance.new("Part", workspace)
			beam.BrickColor = BrickColor.new("Really red")
			beam.FormFactor = "Custom"
			beam.Material = "Neon"
			beam.Anchored = true
			beam.Name = "TestHit"
			beam.Locked = true
			beam.CanCollide = false
			beam.Massless = true
			local distance = (P.SearchPart.CFrame.p - position).magnitude
			beam.Size = Vector3.new(30, 1000, distance)
			beam.CFrame = CFrame.new(P.SearchPart.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
			game:GetService("Debris"):AddItem(beam, 1)
		end

		if part and part.Parent ~= nil and part ~= beam and (part == workspace.Terrain or (part.Transparency <0.95 and part.CanCollide == true)) then
			return part
		end
	end		
end

Pos is the position that it will fly towards, and right now I don’t know how to make it go around a part, all I can do now is just try to go over it.

	local FIF = FindInFront()
	if FIF ~= nil then
		print("Must avoid")
		Pos = Vector3.new(Pos.X + 10,Pos.Y + 10000,Pos.Z + 10)
	end

I’m guessing I’ll probably need a loop of raycasting? but this is quite a big script right now and script activity is already very high

If you can tell me what I need to do or give me a small function to get around this problem it would be greatly appreciated.

Perhaps pathfinding will do here?
Though pathfinding only works for ground NPCs so you would have to adjust the Y axis of Vector3 to ground level when using the NPCs position.

I was also thinking path finding at first too. You should try it but in the case it doesn’t work you could try adding like rods coming out of your helicopter a certain distance, and run multiple scenarios if any of the parts are touched, it will make the appropriate turn to avoid the obstacle in that direction.

this video will may work Roblox Helicopter AI is Finally Here and it is DEADLY - YouTube