AI Setup
In the Space Combat Kit, two AI prefabs have been provided:
EnemyPilot_SpaceFighter - for space fighter AI
EnemyPilot_CapitalShip - for capital ship AI
To use them:
Drag the prefab into the scene
Open the Game Agent inspector and set the Starting Vehicle field to some ship that exists in the scene
Creating AI
To set up a new spaceship AI:
Create a new game object
Add a Game Agent component and make sure Is Player is unchecked in the inspector
Add a new game object as a child and add a Behaviour Selector component to it
For each spaceship behaviour you want to add, add the component to a child gameobject and drag it into the Behaviours list in the inspector of the Behaviour Selector component.
Behaviour Selector
The primary vehicle input script for spaceship AI is the Behaviour Selector.
This component stores a list of spaceship behaviours, and each frame, tries to run them, starting from the first behaviour in the list. As soon as a behaviour runs successfully, the selector stops until the next frame.
That means that the first behaviour in the list has priority, and the last behaviour has the last priority, and as long as higher priority behaviours are running, lower priority behaviours will not run.
So for example, the space fighter enemy prefab has the following behaviours added to the Behaviour Selector component:
Obstacle Avoidance
Combat
Patrol
This means that:
If Obstacle Avoidance is running (there's an obstacle) other behaviours won't run.
If no obstacles are found, obstacle avoidance doesn't run, selector moves to Combat.
If no targets are around, Combat doesn't run, so selector moves to Patrol
Thus you want to add behaviours in order of priority to the list.
Behaviours
Currently, the following spaceship behaviours have been added to the Space Combat Kit:
Spaceship Move To Position Behaviour
Spaceship Formation Behaviour
Spaceship Patrol Behaviour
Spaceship Evade Behaviour
Spaceship Attack Behaviour
Spaceship Combat Behaviour
Obstacle Avoidance Behaviour
Adding Behaviours
To add behaviours to your AI:
Add components that extend the AI Spaceship Behaviour base class under the AI's behaviour selector
Drag them into the Behaviour Selector's Behaviours list in order of priority (first = highest priority)
Creating Behaviours
To create your own spaceship behaviour:
Create a new script that extends the AI Spaceship Behaviour base class.
Override the Behaviour Update function to add your own code.
In your own code, call the Steer Toward and Move Toward functions and pass the target position to make the ship steer and move toward it.
For example:
Last updated