- Home /
How can you make an AI perform specific movements?
I'm making an RTS game with resource collecting units and when they fill up they take the resources to a droppoff point. The droppoff point is basically a drive thru where when the unit gets to the middle it stops and the resources are taken from them then they continue on. I only would like one truck on the ramp at a time and I don't what the best method is to make the dumptruck move through the drive thru. I could make a couple gameobjects so that the truck can move to their positions one at a time but is there a more convenient way to do this and make sure only one truck is on the ramp at a time? Thanks!!
Answer by Max_Bol · Dec 15, 2016 at 03:06 AM
This is simple thing, but with a BIG chunk of complexity behind it.
First, the thing you got to think about is the path-finding. Depending on how you build your game, there's too many options possible so nobody can help you without knowing what kind of movement the AI will do. For example, a city building management where vehicles follow roads with some exception uses a different path-finding mecanism than a RTS games such as Warcraft/Starcraft. This system is the base on which you might want to know about as it will controls everything related to AI movements in your game.
To give you an example of the 2 examples above:
In a city building game, the cars, trucks or whatever will stop in a line/queue on the road if the road forward is blocked. (Some games, after a while, removes those cars because there's no such thing "turn around" features in the AI of those specific games.)
In a action RTS game, the AI usually "park" themselves around the area if it's blocked. It's similar to the city building game, but instead of being stuck in a queue/line, they get stuck on the "side" from which they came in and flood the area. This kind of game usually have a per-AI path-finding system where each AI has a mini detection system that allow them to know how to get to a specific point in the scene and if the way to it is blocked. If it's blocked, they try a new way and after X tries, they stop at the "closest to the target, but between them and the target" available spot they can detect.
Once you know how the AI manage its movement on the "grid", you can easily set something like a List from which, whenever the "Drive through" is free, it send an "OK you can come in" call to the first truck in the List and removes it from it once it's done. (Whenever a new truck has to come through the Drive Through, it's added to the List, which automatically set him to the lowest position.) Now, the possibilities are endless as you might want to update the "next" truck based on who's closest to the entry point and not a "fixed" list (if they flood a side of the drive-through, this is kinda required to avoid that a truck has to pass through the other because it was higher in the list.) Still, the List is pretty much the start to manage "who's ready to enter the Drive Through".
Answer by ericbegue · Dec 15, 2016 at 08:23 AM
To handle your AI logic, I suggest to have a look a Behaviour Tree. It's an AI technique that handles complex (and simple) logics that run over long periods of time (several frames). Basically your AI behaviour is organized into a tree of hierarchical tasks. I've wrote a scripting framework based on Behaviour Tree ( see Panda BT: www.pandabehaviour.com). Using this tool, the resource drop off behaviour of your trucks would look like:
tree("DropResources")
sequence
MoveTo_NearestDropOff
Wait_UntilRampIsFree
MoveTo_DropOffRamp
Drop_Resources
Exit_Ramp
Each of the tasks ( MoveTo_NearestDropOff, Wait_UntilRampIsFree, ... ) is implemented as a C# function in one of the MonoBehaviour attached to the GameObject. Furthermore, you can visualize the execution of script as it is running, which is valuable for debugging or just inspecting what the AI is currently doing.
If you have questions about using this tool or about Behaviour Tree in general, you are welcome on this thread.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
RTS Style building help 2 Answers
Scaling AI for Many Actors/Agents 1 Answer
AI Programing Resources 9 Answers