Volleyball is a game about interactions between players. But what if you don’t have another human to play with? Well, you need an A.I.
I decided that next gameplay mechanic I implement will be serve receiving. But to receive a ball, someone needs to serve it first. Good news is I already have serving mechanic that looks pretty solid for now. Bad news is that it requires a player control. It would be really awkward to serve and then receive the same serve. Therefore I needed a computer player to serve it for me.
AI rules are pretty simple (for now):
- Throw the ball in the air
- Wait until it reaches maximum height
- Randomize a place where ball should target (with possibility to overshoot)
- Randomize ball curve
Unreal Engine has a really cool feature to implement all kinds of AI behaviors – Behavior Trees. Here’s a screenshot of the tree driving our AI player:
In very simple words – every frame, actions are executed from the top to bottom, from left to right order. Blue rectangles represent “Decorators” which is just a fancy word for conditions. Purple rectangles represents actions. So the flow goes like this:
Every frame go through every single action in top-down left-right order and if condition is met, execute it.
Let me know in comments if you would like to read more in-depth explanations.
And here’s the result:
Pretty cool, huh?
Actually, the hardest part was to be able to switch between manual and AI control. It required refactor of almost all the serving logic (I don’t call it code, because it’s all in blueprints). But now I can easily take control of the player or let the AI drive it. It will come in handy later.
Now that the fundamentals are in, I will start work on actual receiving! Stay tuned!