🔹Fetch a ball
From Scratch!
Last updated
From Scratch!
Last updated
You can create advanced AI behaviours thanks to the Brain feature. Let's recreate the Fetch ball Demo of the Wolf Lite from scratch.
I'll be using a new animal for this (Fox).
The Animal must have the Pick Mode with Pick and Drop Animations/Abilities.
In most cases I use the a clip of the Eat animation for the Pick and Drop animations.. In case I don't have those animations.
Pick and Drop animations are the same animation. Drop is reversed (Speed = -1).
The animal must have the Pick-Drop component set properly.
Check the Pick Drop Tutorial.
If your animal does not have these settings. You need to add it.
1.- Lets use scene "AI Sample Scene Fetch Ball". You can find it here:
This scene already has a First Person Controller(FPC) that can pick and throw balls. It has the Navigation mesh baked.
You can test it out with the wolf lite.
After you finish playing around, remember to disable the wolf lite prefab to do the Fetch logic from scratch.
This PFC has already a Pick and Drop and throw logic already set.
Find your animal prefab on your project and Drag the Animal to the scene.
Enable all AI Components on the Animal. (In this case is on the AI Game Object)
AI should not have the Malbers Input component enabled, since both AI and Player Input will fight for controlling the animal.
The Animal Brain needs to communicate with any components in the scene. The best way to do this is to use global utilities the controllers offers.
One is the Transform Hook, which creates a Scriptable Reference of a Transform at Runtime. For more info, check this Hook Transform tutorial.
The FPC must have a Hook Transform with the Player Asset attached to it:
This will be used for the animal to Identify the Player gameobject.
The ball must have a Hook Transform with the Ball Asset attached to it. This will be used for the animal to Identify the ball gameobject.
The ball needs a Bool Listener to identify if the ball has been picked or not (ID = 1).
Every Listener component works as a "Local Variable". Its a simple Monobehaviour with a variable, that can be identify by the Animal Brain. That bool listener is connected to the Pickable component On Picked By and on Dropped by Events.
Let's create the AI States, Tasks, and Decisions needed to have the Fetch logic from scratch.
To Create a new AI State you can hit the plus button from the Brain Component on your animal, when its set to None.
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
This should be the first AI State. This will be used by the Brain to start.
AI State Logic:
The Animal will search for the ball if the ball is in the ground/not picked. If not, then it will search for the Player.
_______________________________________________________________________________________________________
This AI State requires no Tasks. It will only check if the ball is not picked by anyone, or it will search for the player.
_______________________________________________________________________________________________________
The First Decision is an compound AND Decision with 2 internal decisions.
Look Decision: It will look for the Ball, using the Ball_T transform hook attached to the ball used earlier.
Check Var Listener: it will check if the ball is NOT(False) Picked, meaning is on the ground and its not picked by the animal or the player. This used the Check Var Listener decision attached to the ball earlier with the ID =1.
If the outcome of both decision is true, then it will execute the AI State: Search Dropped Ball
________________________________________________________________________________________________
The Second Decision its a Look Decision.
If the first one does not succeed, it will be to search for the Player:
It will look for the Player, using the Player Transform hook attached to it earlier.
If the outcome is true, then it will execute the AI State: Follow Player
________________________________________________________________________________________________
This AI State is executed right after the First State, meaning the Animal has the Player as the current target.
State Logic:
The animal will move towards the Player with the run speed . if the ball is dropped\on the ground; the animal will go and search for it.
_______________________________________________________________________________________________________
MoveStopTask: With the Task = Move to Current Target, the animal will move towards the current target, which it is the Player (Assigned in the last AI State Look Decision).
Change Speed Task: Changes the speed of the animal to Run.
________________________________________________________________________________________________
The Decision is an compound AND Decision with 2 decisions inside.
Its the same AND Decision used on the Find Player AI State
If the outcome is true, then it will execute the AI State: Search dropped ball. (The ball is in the ground)
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
This AI State is executed right after the First State, meaning the Animal has found the ball and it has the ball as the current target.
AI State Logic:
The animal will move to the ball, and and stop when it arrives to it.
_______________________________________________________________________________________________________
MoveStopTask: With the Task = Move to Current Target, the animal will move towards the current target, which it is the Ball. (Assigned in the last AI State Look Decision)
Speed to Run. Changes the speed of the animal to Run
_______________________________________________________________________________________________________
The First Decision is an compound AND Decision with 2 internal decisions.
Arrive Decision: Check if the Animal has arrived to its current target/destination.
Check Var Listener [bool]: it will check if the ball is NOT(False) Picked, meaning is on the ground and its not picked by the animal or the player. This used the Check Var Listener decision attached to the ball earlier with the ID =1.
This is the same Check Var Listener Decision from the first AI State
If the outcome of both decision are true, then it will execute the AI State: Pick Ball
The Second Decision is an Check Var Listener. It will check for the Var Listener attached to the ball with the ID 1. It will check if the Value is True.
If the outcome is true, then it will execute the AI State: Find Player or Ball.
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
This AI State is executed right after the [Search Dropped ball] AI state; meaning the Animal is moving towards the ball and it has arrived to it.
AI State Logic:
The animal will execute the TryPickDrop logic when its near the ball. Then it will wait for the Pick Up animation to finish and It will search for the player.
_______________________________________________________________________________________________________
MoveStopTask: Task = Stop. The Animal will stop moving.
Message Task: This will send a message to itself to the PickDrop component and it will call the TryPickUpDrop method. This will allow the animal to play the Pick Animation and pick the ball.
_______________________________________________________________________________________________________
The decision is an compound AND Decision with 2 internal decisions.
Check Mode Decision: Checks if the Pick Up Mode has Finished. If it has, then verify the next decision.
Look Decision: It will look for the Player, using the Player Transform hook attached to it earlier.
This is the same Look for Player Decision from the First AI State.
If the outcome is true, then it will execute the AI State: Give Ball to Player.
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
This AI State is executed right after [Pick Ball] AI state. The animal is moving towards the ball and it has arrived to it.
Actually this AI State should be named as Move To Player with ball, since here the animal does not give the ball yet. It moves to the player with the ball picked.
AI State Logic:
The animal Move towards the player [Current AI Target] and it will stop when it arrives to it.
_______________________________________________________________________________________________________
MoveStopTask: With the Task = Move to Current Target, the animal will move towards the current target, which it is the Player (Assigned in the last AI State Look for Player Decision).
This is the same Move to Target task from the Follow Player AI State.
_______________________________________________________________________________________________________
Has Arrived Decision: Checks if the Animal has arrived to the current AI Target (Assigned in the last AI State Look for Player Decision).
If the outcome is true, then it will execute the AI State: Drop Ball.
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
This AI State is executed right after [Give ball to Player] AI state. The Animal has arrived to the player with the ball.
AI State Logic:
The animal will execute the TryPickDrop logic when its near the Player. Playing the drop animation and Logic for the ball.
This AI State uses the same Tasks from Pick Ball
_______________________________________________________________________________________________________
MoveStopTask: Task = Stop. The Animal will stop moving.
Message Task: This will send a message to itself to the PickDrop component and it will call the TryPickUpDrop method. This will allow the animal to play the Drop Animation and drop the ball.
_______________________________________________________________________________________________________
Check Mode Decision: Checks if the Pick Up with the Drop Animation has finished. I
If the outcome is true, then it will execute the AI State: Wait.
‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗‗
This AI State is executed right after [Drop Ball] AI state. The Animal has dropped the ball and finish the Drop Animation.
AI State Logic:
The animal wait 3 seconds and it will reset the whole AI State to the [First Find Player or Ball State] to complete the cycle.
MoveStopTask: Task = Stop. The Animal will stop moving.
Wait: Wait 2~3 seconds.
When the outcome is true, then it will execute the First AI State: Find Player or Ball
Add the First Find Player or Ball State to the Brain of the Animal