> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/plugins-and-integrations/convai-unity-sdk/features/conversation-targeting/custom-targeting-rule.md).

# Write a custom targeting rule

Replace conversation targeting with your own rule when none of the built-in modes fit how your game chooses who to address.

Implement `IConversationTargetProvider` when none of the three built-in targeting modes fit your game — the character a UI list has selected, the one a quest is currently about, or the one standing in a trigger volume. Use this page instead of falling back to `Manual` and rebuilding the parts of targeting that already work correctly.

### Prerequisites

* Familiarity with the model in [How conversation targeting works](/api-docs/plugins-and-integrations/convai-unity-sdk/features/conversation-targeting/how-conversation-targeting-works.md)
* A rule for choosing a target that the three built-in modes cannot express

### Implement the interface

`IConversationTargetProvider` has a single method, `ResolveTarget`, called on every targeting evaluation.

```csharp
using System.Collections.Generic;
using Convai.Runtime.Components;
using Convai.Runtime.Conversation;
using UnityEngine;

public sealed class QuestTargetProvider : MonoBehaviour, IConversationTargetProvider
{
    public ConvaiCharacter ResolveTarget(
        IReadOnlyList<ConvaiCharacter> candidates, ConvaiCharacter current) =>
        QuestState.CurrentSpeaker ?? current;

    private void Start() =>
        ConvaiManager.ActiveManager.SetConversationTargetProvider(this);
}
```

`candidates` lists every character currently in the room, in stable order, and is never null or empty when `ResolveTarget` is called. `current` is the character currently holding the conversation, or `null` when none does.

Return the character the player should be addressing. Return `null` to leave the conversation where it is. Returning a character that is not in `candidates` also leaves the conversation where it is.

### Register the provider

```csharp
ConvaiManager manager = ConvaiManager.ActiveManager;
manager.SetConversationTargetProvider(new QuestTargetProvider());
```

Pass `null` to `SetConversationTargetProvider` to remove the custom provider and restore the behavior configured by `ConversationTargeting.Mode`.

{% hint style="info" %}
A registered provider replaces the built-in modes outright, including `LookAt` and `Proximity`. The `ConversationTargeting` settings on `ConvaiManager` have no effect while a provider is active.
{% endhint %}

### What still applies on top of your rule

A provider only chooses which character to address. Everything that keeps the choice pleasant still applies on top of what the provider returns: the conversation still never moves mid-sentence, it still does not commit to a new target on the first frame it becomes a valid choice, and it still never leaves the player with nobody listening. See the three rules in [How conversation targeting works](/api-docs/plugins-and-integrations/convai-unity-sdk/features/conversation-targeting/how-conversation-targeting-works.md) for what this means in practice.

If `ResolveTarget` throws, the SDK catches the exception, logs it, and leaves the conversation where it is rather than letting one faulty provider take the conversation down.

### Verify the provider is active

Enter Play mode and confirm the conversation follows your custom rule rather than the player's view or position. Check the Live section of the `ConvaiManager` Inspector — it reports the addressed character and the verdict behind the last targeting decision regardless of whether a built-in mode or a custom provider produced it.

### Next steps

{% content-ref url="/pages/IvwhyGLW53mGaa4IUQOv" %}
[Script the conversation target](/api-docs/plugins-and-integrations/convai-unity-sdk/features/conversation-targeting/script-the-conversation-target.md)
{% endcontent-ref %}

{% content-ref url="/pages/O4HC64QMEI0DrwmGFprK" %}
[Conversation targeting reference](/api-docs/plugins-and-integrations/convai-unity-sdk/features/conversation-targeting/targeting-reference.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.convai.com/api-docs/plugins-and-integrations/convai-unity-sdk/features/conversation-targeting/custom-targeting-rule.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
