# Creating a Companion AI

## Creating an Companion AI

Companion AI act very similarly to Aggressive AI. They will attack detected targets who they have an Enemy Relation Type with. However, they will not wander according to their Wander Type. They will instead follow the assigned Target to Follow. This can be done through the Behaviors Component or through code.

To set an AI's behavior, go to the AI's Behavior Component and expand the Behavior Settings foldout. Set the Current Behavior Type to Aggressive and assign the desired transform to the Target to Follow. This can be either another AI or a player.

{% hint style="info" %}
**Important:** When an Aggressive AI has a Target to Follow, it will make all options within the Behavior Settings unusable.
{% endhint %}

<figure><img src="/files/eyzABSX7PR39QpGtGUMC" alt=""><figcaption></figcaption></figure>

Once you have set your desired behavior settings, your AI should be able to follow its set Target to Follow and attack any detected target that has a Relation Type of Enemy. If your AI is not detecting targets, ensure that that you have completed all steps within the [Getting Started](/emerald-ai-wiki/getting-started/included-demo-scenes.md) section of the Emerald AI Wiki.

## Setting a Target to Follow Through Code

There are two ways to assign a Target to Follow through code, if desired.

### Through the EmeraldAPI

The first method is to assign a Target to Follow through the static EmeraldAPI class. The TransformToFollow would be the target you would like the owner of the EmeraldComponent to follow.

```csharp
EmeraldSystem EmeraldComponent = GetComponent<EmeraldSystem>();
EmeraldAPI.Detection.SetTargetToFollow(EmeraldComponent, TransformToFollow);
```

### Through the Detection Component

The second method is through the AI's own detection component. The TransformToFollow would be the target you would like the owner of the EmeraldComponent to follow.

```csharp
EmeraldSystem EmeraldComponent = GetComponent<EmeraldSystem>();
EmeraldComponent.DetectionComponent.SetTargetToFollow(TransformToFollow);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://black-horizon-studios.gitbook.io/emerald-ai-wiki/emerald-components-required/behaviors-component/creating-a-companion-ai.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
