AI Spaceship Behaviours

The AI Spaceship Behaviour class provides a base class for creating spaceship AI behaviours, and is used for the following behaviour components:

  • Obstacle Avoidance Behaviour

  • Spaceship Combat Behaviour

  • Spaceship Attack Behaviour

  • Spaceship Evade Behaviour

  • Spaceship Patrol Behaviour

  • Spaceship Formation Behaviour

  • Spaceship Move To Position Behaviour

The main features of this base class are the PID controller and max rotation angles settings, which enable you to control the way the spaceship maneuvers during each behaviour.

PID Controller

The PID (Proportional - Integral - Derivative) controller is a type of controller used in robotics and control systems to enable an object to steer or move toward something by minimizing something known as the 'error value'.

It's not necessary to know about PID controllers to get everything you need out of the AI, and in most cases, leaving it at the default values will work for any use case.

The Error Value

In very simple terms, the error values that the controller seeks to minimize are:

  • For steering, the difference in angle between the vehicle's current direction versus the direction you want it to be facing

  • For movement, the distance between where the vehicle currently is versus where you want it to be


  • The Proportional value is how much the vehicle steers/moves as a result of the difference between the current direction/position compared to the desired direction/position

  • The Integral value is how much the controller output accumulates as a result of the difference not being reduced fast enough

  • The Derivative value is a 'braking' value that provides an opposing pressure as the error value approaches zero - to minimize swinging around the target value

Max Rotation Angles

You may want spaceships to limit the amount that they rotate on a specific axis while maneuvering, for example space fighters might roll hard while capital ships simply turn sideways.

You can customize the maximum rotation values around each axis (local X, Y and Z axes) by setting the Max Rotation Angles field in the inspector of any AI Spaceship Behaviour component.

  • X-value is pitch (nose up/down)

  • Y-value is yaw (nose left/right, in most cases leave this at 360)

  • Z-value is roll

Creating Your Own Spaceship 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;

Last updated