How to set a distance for NPC barking in 2D?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Eldritch_Horror
Posts: 28
Joined: Sun May 28, 2023 8:30 pm

How to set a distance for NPC barking in 2D?

Post by Eldritch_Horror »

How would I best resitrict NPC barking to when the player is within a certain distance in a 2D game? I tried a variable set by a trigger, but it didn't work.
User avatar
Tony Li
Posts: 22130
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to set a distance for NPC barking in 2D?

Post by Tony Li »

Hi,

1. Add a Circle Collider 2D to the NPC. Tick Is Trigger. Set the radius to the max distance at which the player can hear the bark.

2. Add a Range Trigger component to the NPC. Set Conditions > Accepted Tags to Player (assuming your player GameObject is tagged "Player".) Assign your bark-related component(s), such as Bark On Idle, to the Components list.
Eldritch_Horror
Posts: 28
Joined: Sun May 28, 2023 8:30 pm

Re: How to set a distance for NPC barking in 2D?

Post by Eldritch_Horror »

The bark is still active from the games start. The collider is also acting to start the dialogue/conversation with the character.
Attachments
Clipboard01.png
Clipboard01.png (542.77 KiB) Viewed 210 times
User avatar
Tony Li
Posts: 22130
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to set a distance for NPC barking in 2D?

Post by Tony Li »

Hi,

Disabled the Bark On Idle component at design time by unticking the checkbox next to the Bark On Idle component's heading.

If your Dialogue System Trigger is set to OnTriggerEnter, that would explain why it's starting the conversation when you enter the trigger collider. Here are two ways to resolve it:

1. If you're using AC's interaction and a Third Party: Dialogue System Conversation action, you probably don't need to the Dialogue System Trigger at all. You can remove it in this case.

2. Or, if you need it to trigger OnTriggerEnter, create an empty child GameObject. Move the big range trigger collider and the Range Trigger component to the child GameObject.
Post Reply