In general, I want to create a flying drone in space that can notice the player, fly closer to him and at a certain distance it will fly around him or hover in one place if there is something around and shoot at him. I already guess how to make a shooting system, but I have no idea how to make its movement system. Most likely, CFrame will be used, because bugs and problems can arise with body movers. How can I make an AI for such an NPC so that it flies around obstacles and does not crash into them. What is better to use raycasting, modify the usual pathfindingservice, or neither. Thanks in advance for every answer
will there be obstacles in it’s way?
Hello. Ye it will be like big asteroids or something., maybe even flying metal sheets
i suggest making an anchored platform where the player is flying and delete it on the client with a local script then use pathfinding service to map a path between 2 points on the platform. Someone may have a better suggestion but that’s the only way i can think of rn to make pathfinding service work in the air
This could be a solution if enemy levitate, but in my case he can move like everywhere and drone can fly anywhere, not locked on one height
For flying enemies in space, you might just wanna skip using PathfindingService entirely. It’s really just designed for ground navigation with navmeshes.
You’ll wanna use a form of 3D navigation:
Basic movement
-- direction to player
local direction = (player.Position - drone.Position).Unit
-- is path is clear?
local raycast = workspace:Raycast(drone.Position, direction * range, params)
if raycast and raycast.Instance ~= player then
-- obstacle detected, pick avoidance direction
-- cast rays in multiple directions to find clear path
else
-- move toward player
end
For hovering at a distance:
local distance = (player.Position - drone.Position).Magnitude
local targetDistance = 50 -- your hover distance
if distance > targetDistance then
-- move closer
elseif distance < targetDistance * 0.8 then
-- back away (the 0.8 prevents jittering)
end
For obstacle avoidance you’ll wanna cast rays in a cone pattern around the direct path. It gets a bit more complicated here but you can look into using cross product to get perpendicular directions for smooth strafing around objects.
If you have any other questions lmk
you could either make the platform move with the drone or move the drone while using rays to check if a path is blocked, and if it is the drone can either go left/right until the path isnt blocked or you can set points the drone can move to if its path is blocked, and it chooses the closest point
Although i am not the best at anything pathfinding related, i believe you will have to make your own pathfinding algorithm.
You might want to check out A* Pathfinding, or if you don’t want to read: explanation
Another (and probably easier to learn) algorithm is Dijkstra
or as others above me mentioned, use an invisible part, hacky but works.
This sounds like a good idea. But I have one more question.
For example drone will get in a two ways hallway or something so he will have 2 decision, but how I can let him know which of them lead to player and which of them is empty.
This sounds like an unrelated issue to your post, we would have to know more context in order to help, i suggest you come back to this when you get to that part, or if already at it provide more context.
If you’re asking about navigating through confined spaces like a maze where the drone can’t see the player and can’t escape from it, then you’d probably need actual pathfinding like @lavasance mentioned. We’d need more context to understand how complex you need your pathfinding to be.
I thought about it a bit and realized that I really don’t need such advanced pathfinding, thank you for the solution and ideas!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.