[HOWTO] How To: Only Interact If Player Is Facing NPC

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 22000
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Only Interact If Player Is Facing NPC

Post by Tony Li »

This subclass of DialogueSystemTrigger checks the angle between the player's forward heading and the NPC that the player is trying to interact with. It will only fire the trigger if the angle is less than 30 degrees. You'd typically add a script like this to the player along with a ProximitySelector component.

CustomDialogueSystemTrigger

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class CustomDialogueSystemTrigger : DialogueSystemTrigger
{
    public override void TryStart(Transform actor, Transform interactor)
    {
        Vector3 vectorToNPC = transform.position - interactor.position;
        float angleToNPC = Vector3.Angle(interactor.forward, vectorToNPC);
        if (angleToNPC <= 30f) // Is player facing within 30 degrees of the NPC?
        {
            base.TryStart(actor, interactor);
        }
    }
}
Post Reply