Objective
In the previous tutorial, we have basically implemented path navigation and obstacle avoidance .
But then we did not let our character learn to think, he just mechanical to complete some steps, this does not reflect the rain plug-in intelligence.
A character he should have several different states, standby, patrol, Discovery, chase, attack and so on. and be able to think and react to your own behavior.
Transitions between states require signals , just like in our real world, where the signal source is visual and auditory.
So can we add visual and auditory to the AI role?
Answer: Of course, we can, in order to make our role more intelligent, let us implement this function.
Scenario Preparation
I set the scene as follows
Green Capsule body: player
Red ball: Enemy
The enemy will do Pingpong movement (patrol) along the path in the scene I have arranged.
Note: We set the hierarchy for all walls (obstacles): wall, set the level to calculate collisions in the properties of the navigation grid. (because in the scene if there are flowers or other objects that we do not want to collide with, we should not calculate the navigation)
Behavior Tree Toggle
Before we start, we need to know how to do the behavior tree switchover in rain, we open the enemy's AI,
Choose the bulb this option, called memory here, in rain you need to save the variables declared here .
We add a variable model of type string and assign a value of patrol
Create a basic behavior tree myai_01
Create a total behavior tree Myai
Change the default SEQ (sequential) node to par (parallel) parallel node.
and use the constraint node to judge
Binds the myai_01 behavior tree for the current behavior tree
Specify the behavior tree in the brain thinking mode
Run we found that in the behavior tree we have already judged by the constraint node to bind the myai_01 behavior tree to the current behavior tree.
In this way we can set up multiple sets of AI for the character, select and transform under certain conditions.
Add Visual to Enemies
Open rain on our enemies and add a monitor under the eye icon (Note that we add a visual monitor at this point)
After adding we found that our enemies are more than 2 green rings
The range of this ring is the visual range of our enemy.
In this case, we can adjust the visual range parameters in the Inspector panel.
Range: Radius
Horizontal Angle: Horizontal angle
Vertical Angle: Vertical angle
We adjust the parameters to simulate the visual range of the enemy. (At this point I set the z axis to be the front of the enemy)
Now let's add a signal to the player (in rain, it's called the solid entity)
Select Player to add
Add a visual signal that you can see on the panel after adding
The myai_01 behavior tree is added on the patrol according to the path, run, already able to switch the behavior tree and execute according to model.
The fulfillment of the chase behavior
Create a navigation target and mount it to the player's object.
Create a behavior tree myai_02 as the chase behavior tree and set the target.
Add the child behavior tree on the Myai total behavior tree, and set it up as above. model is pursuit
Now we can switch between the 2 states. :
Increase attack status (using custom scripts)
Create an attack behavior Tree myai_03
Create a custom Action node and create a customized script test
Open Test We see that it inherits from the Rainaction and implements several methods of the superclass.
usingRAIN. Action;usingRAIN. Core; [Rainaction] Public classtest:rainaction{
Called when the node begins execution Public Override voidStart (RAIN. Core.ai AI) {
Add a Debug message Debug.Log ("attack!!"); Base. Start (AI); }//Returns whether this operation succeeded or failed Public OverrideActionResult Execute (RAIN. Core.ai AI) {returnactionresult.success; }//Called when the node is stopped Public Override voidStop (RAIN. Core.ai AI) {Base. Stop (AI); }}
Run and switch the model to attack and discover the console output.
Our custom node has been executed.
In This section we mainly describe how to switch behavior trees, how to define how variables are custom nodes,How to patrol, Chase, attack.
More content, so the visual and auditory monitoring in the next section to introduce.
I hope this article can help you, you can pay attention to I have received the latest articles I published notice.
Unity Rain Ai Plug-in basic use (ii)