Page 1 of 1

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

Posted: Thu Jun 02, 2022 5:00 pm
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);
        }
    }
}