Making an Interaction Prompt appear and disappear when a player look at an item

Hey All,
In my project I have objects for the player to interact with (Doors, Items to pick up, etc…) and what I’m wanting to happen is when the player looks an item a “Interaction Prompt” pops up with different text depending on the item -“Collect” if its an item to Pick up, “Open” if its a door etc.
I also want it so that when the player looks away from the object the “Interaction Prompt” disappears.

so far I’ve made a “Base Interactable” that all my objects inherit from.
This “Base Interactable” implements two interfaces, a “Interaction interface”, and A “Display Prompt” Interface.
Both have various functions for validation and to allow me to decide if I want to turn the option to interact on or off at anypoint.
The Base Interactable also have a UI widget attached to it so that the “Interaction Prompt” is displayed on the item rather:

Anyway I’ve managed to get to the point where I have the “Interaction Prompt” appearing and disappearing when the player is looking at the item and when looking away, but I’m just wondering if I could be doing this a better way?

So far I’m running a Line trace on the “Event Tick” tick, if it hits an object is validates if its an interactable object and if it can be interacted with, if both those conditions are true I’m storing a reference to the item that’s being looked at (for use if the player decided to interact with the item)and then I’m setting the visibility of the Item Widget to true, when the player looks away (Raycast hit is false) then I’m setting the widget on the stored reference to not be visible and clearing the reference

Essentially I’m little concerned about how much I’m running on the “Tick Event” and if I could make this cleaner? or if there is a better way of doing this all together?
Any help would be much appreciated!

Hey @ItsRyan95!

This is a PERFECTLY acceptable use of tick, in fact I’d say this is the sort of thing tick is intended for.

Really, all you’re doing is a trace Unless there’s something new to interact with OR you need to remove the prompt.

I’d say the only thing I’d do to clean it up is on “True” after the line trace, check if it is == the Interactable object you ALREADY have referenced, and if it is NOT the same object, proceed, and if it IS the same object, just end it there. That’ll remove any additional re-setting of the variable on tick so it only happens once until either you look away or look at something else.

Once you do that, the impact of tick on your gameplay is highly positive, and the impact of tick on your processing is very, very low (line trace is insanely cheap.)

Now, if you didn’t have those branches in there preventing extra code from executing, that’d be a different story, but this is great. :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.