This page describes how the AI scripts of the Corgi Engine work.

Introduction

Jekyll
A bunch of AI controlled characters walking around in their natural habitat.

Right now the engine includes a few basic AI scripts for your enemies (or friends). These are components you can add to your characters (simply via the AddComponent button at the bottom of the character’s inspector), and it’ll make them perform actions without the need for input from the player. To use them, you need to have a Character setup properly, with (at least) the required components for each AI script. Each AI script may require certain particular Abilities, but all of them require that you have a CorgiController and a Character. You can also try combining them : for example you can add AI ShootOnSight and AI Walk to a Character and it’ll be able to walk around and shoot at your player. More AI scripts are coming soon to help you create more diverse enemies, but feel free to create your own AI scripts, it’s really easy and fun to do.

AI ShootOnSight

This component requires a CharacterHandleWeapon component. What it does is cast raycasts to its left and right, targeting the objects on the layers you’ll have specified in its inspector. If one object hits these raycasts, the AI will shoot at it using its current equipped weapon.

AI Walk

This component requires a CharacterHorizontalMovement component. It’ll allow your AI character to walk. You can specify the desired behaviour in its inspector. From there you can have it patrol (walk randomly and change direction when hitting a wall or optionnally a hole), or walk on sight, which means the AI will “look” (using raycasts) for objects on the layers you’ll have specified in its inspector. If a raycast hits such an object, the character will move towards it.

Creating a very simple AI character

  1. in Unity 6000.0.23f1 (or higher), create a new project and import Corgi Engine v9.2 via the Package Manager
  2. open the MinimalLevel demo scene
  3. from the project panel, drag a Platform1000x100 prefab into the scene, position it at 0,-1,0
  4. create an empty game object, position it at 0,0,0, name it MyTestCharacter
  5. create a new empty child game object to the MyTestCharacter node, add a SpriteRenderer to it, set its Sprite to Adventurer_0, name it MyModel
  6. on the MyTestCharacter node, add a Character component, press the AutoBuild AI Character button at the bottom of it
  7. drag your MyModel into the Character’s inspector’s CharacterModel field
  8. edit the BoxCollider2D’s size to 0.8,0.8
  9. on the AI’s AIWalk component, check AvoidFalling:true
  10. press play

AI Follow

This component requires a CharacterHorizontalMovement component. Add this script to a Character to make it follow (or try to follow) the main Player character. If it has a CharacterRun component it’ll try to run when far from target. If it has a CharacterJetpack it’ll try to jetpack over obstacles to reach the target, and if it has a CharacterJump component it’ll try to jump over obstacles.

Surface Crawler

A standalone AI component with no prerequisite, it will let you have an object follow a surface, like a spider crawling on walls. From its inspector you’ll be able to define what direction the crawler should take, the speed at which it moves, what surface to “walk” on, and precisely configure its various raycasts based on your context and crawler specifics. You can see an example of that in action in the RetroMountains demo scene, with the RetroFireballCrawler.