Character Controller Pro (1.4.x)
  • Introduction
  • The package
    • Content
    • Versioning scheme
    • Importing the package
    • Using the package
    • Known issues
  • Fundamentals
    • Core
      • Character
      • Character body
      • Character actor
        • States
        • Stable movement features
        • Velocity
    • Implementation
      • Character state controller
      • Character state
      • Character brain
  • How to...
    • Core
      • Create a basic character
      • Add behavior logic to your character
      • Move the character
      • Rotate the character
      • Leave grounded state (e.g. jump)
      • Change the size of the character
      • Disable collisions (ghost)
      • Use the character information (with examples)
      • Use root motion
      • Detect a character using a "detector"
      • Add a static one way platform
      • Add a dynamic one way platform
    • Implementation
      • Organize the character hierarchy
      • States
        • Add and configure the state machine
        • Create a state
        • Transition from one state to another
        • Handle animation
        • Modify an IK (inverse kinematics) element
      • Actions
        • Define your own actions
        • Use the character actions
        • Use a custom Input Handler
        • Use the new input system
        • Create your own AI movement logic
Powered by GitBook
On this page
  1. How to...
  2. Implementation
  3. Actions

Create your own AI movement logic

When the character brain is set to AI, the actions stop being updated by the input handler (input devices). Now these actions need to be defined by code. This is what AI is all about, simulating a machine "pressing buttons".

In order to set up an AI character from zero you need to:

  1. Change the brain mode to "AI" in the CharacterBrain component.

  2. Add an CharacterAIBehaviour component to the character (wherever you want).

  3. Assign the CharacterAIBehaviour to the CharacterBrain.

Human and AI move around using the same components, there is no need to add a NavMeshAgent or something similar. Also you don't need a NavMesh for this to work, although you can use one if you want (see the AI Follow behaviour).

The AI behaviour

The brain will use the character actions from the current AI behaviour. Basically this behaviour task is to define this action frame by frame, as simple as that.

To create an AI behaviour you need to derive your class from CharacterAIBehaviour.

public class YourAIBehaviour : CharacterAIBehaviour
{
    // virtual (optional)
    public override void EnterBehaviour( float dt )
    {
    }
    
    // abstract (mandatory)
    public override void UpdateBehaviour( float dt )
    {
    }
    
    // virtual (optional)
    public override void ExitBehaviour( float dt )
    {
    }
}

From there you can create your own custom logic. For instance, to simulate the AI running forward:

public class RunForwardBehaviour : CharacterAIBehaviour
{
        
    // abstract (mandatory)
    public override void UpdateBehaviour( float dt )
    {
            // Run is a Bool action (e.g. a button or a key)
            characterActions.run.value = true;
            
            // Movement is a Vector2 action (e.g. WASD)
            characterActions.movement.value = new Vector2( 0f , 1f );
    }
    
}

You can check the character actions preview from the CharacterBrain inspector.

PreviousUse the new input system

Last updated 4 years ago