"Look at object" event in Blueprint

Hi guys,

I’m currently trying to figure out how to trigger an event (something simple like making a light visible) when the crosshair (i.e center of the screen) is looking at an object.

Basically like a CursorOver event type thing, but for the crosshair. I assume it will be a bit more complicated than a cursor hover event though haha.

If anyone could provide some help that would be great.

An added bonus would be if I could specify a minimum distance away from the object that the player has to be to trigger the event too.

Quick ‘n’ Dirty sketch of what I want (obviously simplified a lot):


I saw this thread but I’m not sure if they were headed in the right direction, and the OP of that thread never actually got a working example from the looks of it.

Cheers,

I know this is not the perfect solution, but you could you this one:

Make a Timer for 100ms or similar. Then do a Single Trace Line and check, if the HitObject is your object. If it is, turn the light on, if not,turn it off
If you can’t really imagine what I mean, I could do a sample BP :slight_smile:

A sample BP would be really helpful since I’m having a bit of trouble figuring out the Single Line Trace.

Here’s an example of a setup you could use inside of a place-able Actor Blueprint that has a Point Light as a component:

Basically, every tick (frame) this blueprint does a line trace from your Player Character’s location to a location straight in front of it that is determined by the forward facing vector * the distance you want to trace out (this is where you could set a distance limit so it only activates the light if you are close to it). The trace is then checked to see if you hit the object that’s doing the tracing and, if so, turns on the light.

Hopefully this enough to get you started!

Thank you very much! I will give it a go when I get home from work.

It doesn’t seem to work…

Make sure the object that you’re checking whether the crosshair is over generates hit events and blocks the visibility channel. Not all objects do this by default.

I just want to point out that in addition to Steve Allison’s solution. If you’re character is traversing the world space, the Player Direction and Distance is not enough to calculate the Line Trace. Make sure after the X node to add a Vector + Vector not that connects the X with the Player Location. This will allow you to trace even if the player is moved from their spot.

The only reason why I brought this up is the fact that I found Steve’s solution useful but I faced a problem that wasn’t present in his example. Hope this helps any lurkers looking for help.

Okay so if you are using the First Person Blueprint Template:

I created a Simple BP with a Static Mesh of a Lamp & and a Pointlight. My Root element is a capsule component.
A Function with a Boolean Parameter inside the Lamp sets the visibility of the Pointlight to the value of the boolean

The rest of the VCode is inside the MyCharacter BP fired by the Event Tick

  1. Create a new Actor* Variable in the MyCharater BP
  2. you need to correct where the Crosshair is because its not completely renderd to the middle of the screen


The rest is pretty straight forward:

and

as for Part 1 its a simple Line Trace but we need to make the trace come out of our “face” either we get strange results because the trace is not exacly where we are looking at.
We need to raise the Z value of our starting Vector to the Z value of our camera component wich is 64 by default.
Oh and if you want to lower or raise the range of the trace alter the * 1000 node

Part 2 checkts if our trace did hit something if not we check if our Last Lamp Actor var has a value if so we simply turn the light off (we may need to set the light actor to nothing after that… forgot to do that)
if we hit something we turn of the light of the last lamp we traced and afterwards we cast the actor we just hit to our Lamp Class and turn the light on if that was successfull

this should be everything you need to work with

(as you may have noticed english is not my native language… but i hope its still comprehensive :slight_smile:

Superbad screen capture of my solution:

I’ve got it set up slightly differently. I never use a missed trace to change anything. There is an almost unnoticeable delay after looking away from the item, but I find it scales easier to multiple different items.

I have the trace from the first person camera [using location and loc + (x*forward vector) as start and end] send an event to the object through a blueprint interface, which then turns on a lamp with a do-once and then a retriggerable delay that is longer than the trace interval (.2 sec seems to still work, much shorter will cause it to flicker), to turn off the lamp when it no longer gets any additional traces telling it to be on. Once it’s turned off, I reset the do-once to turn it on for the next time it’s looked at. This way you can choose whether it turns off (almost) immediately or after a little while depending on the delay length. And it shouldn’t ever stay turned on because of weird trace hit results or fails not firing as intended.

This is the event on the item, after the line trace (per tick) from the player causes it to fire on that item.

Our version is camera relative, and is called on pressing the E key on the keyboard…

Can you please explain me how did you get switch light input in part 2?