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:

  1. Drag the prefab into the scene

  2. 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:

  1. Create a new game object

  2. Add a Game Agent component and make sure Is Player is unchecked in the inspector

  3. Add a new game object as a child and add a Behaviour Selector component to it

  4. 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:

  1. Obstacle Avoidance

  2. Combat

  3. Patrol

This means that:

  1. If Obstacle Avoidance is running (there's an obstacle) other behaviours won't run.

  2. If no obstacles are found, obstacle avoidance doesn't run, selector moves to Combat.

  3. 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:

  1. Add components that extend the AI Spaceship Behaviour base class under the AI's behaviour selector

  2. Drag them into the Behaviour Selector's Behaviours list in order of priority (first = highest priority)

Creating Behaviours

To create your own spaceship behaviour:

  1. Create a new script that extends the AI Spaceship Behaviour base class.

  2. Override the Behaviour Update function to add your own code.

  3. 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:

public class MySpaceshipBehaviour : AISpaceshipBehaviour
{
    public override bool BehaviourUpdate
    {
        if (!base.BehaviourUpdate()) return false;
        
        SteerToward(somePosition);
        MoveToward(somePosition);
    }
}

Last updated