Check if player is facing a object before

Goodday!

I’m trying to check if a player is facing the object before it’s displaying a menu (widget), but I’ve no idea how to do it. The code is now checking if the player is inside of the triggerbox.


I want to check if the player is looking at the door before the menu is shown.

Add a LineTraceByChannel before creating the Menu.
Start point is the player character location. End point is the player characters forward vector multiplied by (for example) 1000.

This has an output called “Return Value”
So first checkout with a Branch, if something was hit (true) or not (false).
If true, break the result struct.
You’ll find the “Other Actor” Output.
Cast this to the blueprint of your door.
If cast isn’t failing, the player is looking at the door… So show the widgets.

Hey Patrick!

Thanks for reacting. I’m pretty new to UE4 so I’ve been trying to recreate what you explained but I’m having a hard time. Did is what I did so far:

I can’t seem to find the “Other Actor” output. If you could try to make it a little bit easier for me to understand you would be helping me alot. Thanks in advance!

You need to drag a line from the “Out Hit” and type in the context window “Break”

The break command breaks the Hit Strict into his variables… There you’ll find the Other Actor

I’m encountering a problem.

  1. When player is not looking at the door the widget isn’t casted. (When I turn the bracket values around then the widget does get casted when the player isn’t looking at the door)
    (Everything else works perfectly)

You dont want to execute your branch from the failed output of the door cast.
This is executed when there is no door hit.

Take the execute out with no name

But… I would give you the advice, to switch the branch and the cast nodes… So you first check if anything got traced, and if true, you cast the actor to your door… And from that show your widgets…

And branch false AND cast failed, lead into the removing of the Widgets.

PS:
Don’t remove Widgets and create them again.
Better create a function to show and hide a widget in your PlayerController, and store a Reference there.
This way, you save some creation time by simply switching visibility true/false.

Thank you so much! It’s working perfectly!

1 Like

The only thing I’d like to add is that it checks if the player is rotating when inside of the triggerbox. Because when you enter the triggerbox without looking at the door and then look at the door the menu doesn’t pop up. How would I do that?

You will need the two Overlap Events of your doors trigger box.
OnActorOverlapBegin
OnActorOverlapEnd

A bool variable in your PlayerController.
And a Tick Event in your PlayerController.

In the Begin Event, you set the bool to true
In the End event, you set it to false.

In your tick event, you get the bool and do a branch condition.

If true, you fired the trace event as you’ve done it l. BUT: directly after your door cast, you add a DoOnce Node. And on its Output, you put your Widget Showing code.

Back to the branch, right after your Tick Entrance. On thats false Output, drag a line to your widget hiding stuff. Again, on the beginning of that Task, put a DoOnce. After hiding the Widget, drag a new execution Line to the >Reset< Input of the DoOnce.
And, from the end of the Widget Showing Code, drag an execution line to the >Reset< of the hiding DoOnce.

DoOnce can only be fired once, if it won’t get a reset execution.
So your widget can only be set visible once, if not being resetted via the hiding… And vise versa.

And the tick event only runs, when the door trigger bool is set true.

What is that all doing:
When the Player enters the Trigger, he continuously starts firing traces, until he exits the trigger.
Each trace checks for doors to cast and change the visibility.

Here are some roughly made Graphs:
The Door Graph


An Event in the Player Controller, to change the bool

The Tick Graph in the PlayerController

Awesome thanks you so much for your help!