Using Line Trace for Blueprint Interface

If this question has been posted elsewhere and answered, I have yet to find it.

Use Case: I am using a single line trace (being run on a custom event, powered by tick) to find out if the player is looking at anything he/she can interact with (in this case just to show the name of the item). The trace itself works find as in it will find the object and trigger the blueprint. However, since the event fires at the speed of tick, it is CONSTANTLY firing the blueprint.

What I am trying to figure out is a system where once a player’s trace triggers the blueprint, only do that blueprint one time, until the player is no longer looking at the object via trace. I only want the blueprint to fire once WHILE looking at the same object via trace. If the player is no longer looking at the same object, trace as normal again until i hit something else.

I hope I am explaining this properly and that someone understands me. Is there a way to find on TraceEventHit and TraceEventOut like a volume trigger, because that would solve everything for me. I need to be able to SHOW item name while the player is looking at the item, and HIDE the information when the player is NOT looking at it.

Still trying to figure this one out. Anyone have any ideas?

Hey GabrielDamato,

This is similar to a question I answered on the forums a while back: "Look at object" event in Blueprint - Blueprint Visual Scripting - Unreal Engine Forums

For your particular setup though, this is a chunk of code you could you put in each your placed objects (or put in an overall parent actor that you base all of your placed objects on):

Basically, whatever you want to execute only once you include after the Set BeingTargetted to true boolean with the comment box–that setup should only execute once per “look”, and the boolean is reset anytime your line trace doesn’t hit anything or if it hits something that isn’t itself (since this is inside the actor you would be looking at).

If you have any questions about this setup, just let me know!

Cheers,

-Steve

Edit: Just incase

So i have played around with this code, and it doesn’t perform quite like I need it to. Since I am running this on the Player blueprint, it never gets past the == self branch. I see that this is designed for the OBJECT to look for the PLAYER, but how would we reverse this so that the player is looking for objects to interact with. Bear in mind, I will have TONS of objects running this script on trace hit, so the solution as is would be very taxing on the system. Any thoughts on how I might achieve that with this basic structure?

Oh, also this is a really brilliant solution you have come up with so far. Thank you so much for sharing your script. It has certainly given me new avenues to explore as I try to solve this problem!

So, I did actually figure it out (for now). I thought that I would post my solution back to the community, in case someone else runs along it and finds it useful. BIG props to Steve Allison for giving me a jumping off point.

Following the blueprint, here is how it works: Draw the trace however it works best for you (I am using it off a custom event, powered by EventTick). If the trace hits something, we will get that object and store it to a local variable called “Object Hit” (take the Hit Actor node to make sure that the object we hit is the object we set in the variable). For good measure, we will set the bool “Player Is Targeting Object” to false right here if we have not hit an object to make sure that when we hit nothing, we reset it to false.

Next, we will check to see if the object we hit implements our customer Blueprint Interface. If not, reset the “Player Is Targeting Object” to false again for good measure. If the object we hit DOES implement the proper BP Interface, then we will check to see if the last object his was the same object hit (since we are firing this script at the speed of tick, I found this to be a helpful branch).

So at this point the script knows that we HAVE hit an object, it DOES implement the proper BP Interface, and we are STILL looking at it. Moving onto the next branch (via the original script Steve Allison provided), the first time we hit this node the “Player Is Targeting Object” to false, so it allows us to continue. Immediately set the “Player Is Targeting Object” is set to true (to stop the event tick from firing further behind us), and only then do we send our BP Interface a Message.

Sorry if the BP below isn’t pretty, but it works. It should be noted that using this script will only allow the BP Interface Message to fire ONE time, until the player targets another object, or no object at all. This will fire the message each time the player “looks” at the object in question. From here, you would build the interaction of your object on it’s own BP.

Again, thanks to Steve for his original system, without which I could have never figured this beast out. Cheers mate, I give you a virtual high five.

If anyone can think of a way to do this cleaner, I would be interested to see how you got it done, or any modified versions of it working differently. Hope some other n00b finds this helpful!