Page 1 of 1

adding another test to the proximity selector (facing angle)

Posted: Sat Oct 24, 2020 11:12 am
by Strook
Hello,

I want to use the proximity selector in my game to be able to interact with usables because its the one that make the most sense compared to the raycast selector !

However, i would like to add a small feature to the proximity selector :

I want to check if the player is in the trigger AND facing the usable to be able to use it;


I would be interested to know what is the easiest and cleaniest way to implement that ? I have the whole logic for the facing part (using a Dot Product between player forward and usable forward), but im not sure where to put it

I tried creating a new component that disable/enable the usable when the player is not facing, it works but not perfectly


thank you !!

Re: adding another test to the proximity selector (facing angle)

Posted: Sat Oct 24, 2020 1:05 pm
by Tony Li
Hi,

No need to write any code. Just change the collider that the Proximity Selector uses. For example, you could add a Box Collider trigger that's positioned just in front of the player.

However, if you also want the usable object (e.g., NPC) to be facing the player, you could add a script to the usable object. Hook into Usable.onSelect() and onDeselect() to know when the player has selected it. Then, in Update(), set the Usable component enabled or disabled based on whether they're facing or not:

Code: Select all

Usable usable;
bool isSelected;

void Awake()
{
    usable = GetComponent<Usable>();
    usable.events.onSelect.AddListener(() => { isSelected = true; });
    usable.events.onDeselect.AddListener(() => { isSelected = false; });
}

void Update()
{
    usable.enabled = !isSelected || IsFacingPlayer();
}

Re: adding another test to the proximity selector (facing angle)

Posted: Sat Oct 24, 2020 1:48 pm
by Strook
hello and thanks for the reply

the first options seems perfect to me, what is the best way to change the collider used by the proximity collider ? Can i just add a child object with a collider, and add the proximity collider to it, or do the proximity collider component has to be on the player object ?

EDIT : this is what i did and it seems to work fine, please let me now if there any other way, but it work fine ! Thanks a lot for the help

Re: adding another test to the proximity selector (facing angle)

Posted: Sat Oct 24, 2020 2:23 pm
by Tony Li
Hi,

A child object is fine. Make sure to assign the main player object to the Proximity Selector's Actor Transform field.