Skip to content

AI

The framework has three small AI scripts: one for animals that wander, one for animals that react to the player, and one for an enemy that hunts the player with pathfinding. This page covers what each one does, the fields you tune, and the steering component the pathfinding characters move with.

Concept

The AI scripts sit where a player controller would. They decide where to go, and other components do the rest: DirectionalAnimation plays the walk, PhysicCharBehaviour keeps the character out of blocking tiles. See Characters and Vehicles for those two.

Script Used by Moves with
NPCBasicAI Sheep PhysicCharBehaviour
CompanionAI Dog, Cat, Chicken PhysicCharBehaviour
FollowerAI ZombiePlant MovingBehaviour and MapPathFindingBehaviour

The demo2 Farm House scene has the animals. The ZombiePlant appears in demo1 Big Map, spawned by EnemyGen.

NPCBasicAI

A wander loop. The character waits a random time, picks a random direction, walks a set distance, stops, and repeats. When it bumps into a blocking tile it bounces off at a slightly random angle. Adding it also adds PhysicCharBehaviour, DirectionalAnimation and IsoSpriteController if they are missing.

Field What it does
Max Step Delay Longest wait between steps, in seconds.
Min Step Delay Shortest wait between steps, in seconds.
Moving Step Dist How far each step goes, in world units. Shown as a circle around the character when it is selected.
Moving Speed Walking speed in units per second. It overwrites Max Speed on the PhysicCharBehaviour.

CompanionAI

The same wander loop, with a target. At the start of each step, if the target is within Sight Distance, the character heads mostly towards it, or away from it with Flee From Target on. The demo dog follows you around; the cat and the chicken run off.

It has the four fields of NPCBasicAI, plus:

Field What it does
Sight Distance How close the target has to be before the character reacts, in world units. Drawn as a yellow circle when selected.
Flee From Target Run away from the target instead of following it.
Target What to follow or flee from. Left empty, the character uses the scene's PlayerController, so a scene without a player needs this set.

Stopping an animal

Disable the NPCBasicAI or CompanionAI component and the wandering stops: the character stops on the spot and its walk animation pauses. Enable it again and the character starts over with a wait. Before 1.6.1 disabling it did nothing.

FollowerAI

The enemy script. It needs a GameObject tagged Player in the scene, and it finds it by that tag when it starts. Adding it also adds MovingBehaviour, PhysicCharBehaviour, MapPathFindingBehaviour and DirectionalAnimation.

What it does, every physics step:

  1. It checks whether it can see the player: closer than Sight Distance, and with no tile of block collision on the straight line between them when Is Sight Blocked By Blocked Tiles is on.
  2. If it can, it sets the player's position as the pathfinding target and follows the A* path. Once it is within Min Dist To Reach Target it stops following the path and circles the player instead.
  3. If it can't, it carries on towards the last place it saw the player. With no path left, it drifts around in small random circles.
  4. When PhysicCharBehaviour reports a wall on one side, it pushes away from it.
Field What it does
Lock Anim Dir Keep the facing fixed instead of turning with the movement.
Sight Distance How far it sees the player, in world units. Drawn as a circle when selected.
Is Sight Blocked By Blocked Tiles When on, the player is hidden behind tiles with block collision. Walls and fences don't hide.
Min Dist To Reach Target Distance from the player at which it stops pathfinding and starts circling.
Ang Rand Radious Radius of the random circling, in world units.
Ang Rand Off How many degrees the circling direction may change per step. Higher is more erratic.

It turns to face its movement only every few frames and only when moving at more than half speed, so it does not flicker between directions against a wall.

On the ZombiePlant, DamageBehaviour gives it health, and a bullet hit knocks it back through MovingBehaviour. See Shooting.

MapPathFindingBehaviour and the A* search are covered in Pathfinding.

MovingBehaviour

A steering component, after the autonomous agents chapter of "The Nature of Code". Each physics step it adds the accumulated force to the velocity, caps the velocity at Max Speed and moves the object. It is the movement engine for FollowerAI, TouchFollowBehaviour and anything driven by MapPathFindingBehaviour.

Field What it does
Veloc Current velocity. Written every step.
Acc Force collected this step. Cleared after each step.
Radious Arrive starts slowing down inside this distance from the target.
Max Force The largest steering force Seek and Arrive apply in one step. Lower turns wider and reacts slower.
Max Speed Top speed in world units per second.

From a script:

  • Seek(target) steers towards a point at full speed.
  • Arrive(target) does the same but slows down to stop on the point.
  • ApplyForce(force) adds any force of your own, like a knockback.

MovingBehaviour does not check tile collisions. Put a PhysicCharBehaviour on the same object, leave its Dir at zero, and it pushes the character back out of blocking tiles and reports the contact in Coll Flags. That is how the ZombiePlant prefab is set up.

using UnityEngine;
using CreativeSpore.RpgMapEditor;

// Walks to the clicked point in a straight line, with no pathfinding.
[RequireComponent(typeof(MovingBehaviour))]
public class WalkToClick : MonoBehaviour
{
    private MovingBehaviour m_moving;
    private Vector3 m_target;

    void Start()
    {
        m_moving = GetComponent<MovingBehaviour>();
        m_target = transform.position;
    }

    void Update()
    {
        if (RpgInput.GetMouseButtonDown(0))
        {
            m_target = RpgMapHelper.GetMouseWorldPosition();
            m_target.z = transform.position.z;
        }
    }

    void FixedUpdate()
    {
        m_moving.Arrive(m_target);
    }
}

RpgMapHelper.GetMouseWorldPosition uses the map's View Camera. See Camera.