Raycast - Trigger something when looking at an object

Hello guys,

I have a problem that I don’t know how to do in Unreal Engine.

I have this RayCast:

That’s on the FirstPersonBP. It works fine. I have an “interface” called On Interact so I can go to another object and call an “On Interact” event:
dZgePIM.png

What I need however is something different.
I’m trying to see when the player “LOOKS” at a collider in an actor, and if the player looked for X seconds display a text. “Press X to interact”
At the same time, I want when the player looks at an interactible object … and press that X button, I want to do something with that object.

I have a problem to check when the Raycast enters/stays/leaves a collider in another actor. Right now I have a “Door Opening Animation” but that triggers when I look at the whole actor, that has the door & a couple of other stuff. So I want to limit the “raycast look” action to a particular collider that I will create.

Any idea how to accomplish this?

Thank You <3

  • You could add a branch before your OnInteract and check if the class of the HitComponent is the class of your collider

  • You could add an additional trace channel in the project settings, use that trace channel in your LineTrace and let the collision of your collider block that channel while all other actors ignore this channel.

I don’t really understand :frowning:

My BP Actor looks like this:
89b1846dc8140e664e2ade157a2a85b6cfaf82b9.png

My raycast can be seen in the first post. Whenver I use that the whole thing triggers whenever I see “MainEntrance”. Also it seems that the Actors from under don’t get hit by the raycast, only the ROOT.

I want the raycast to trigger when I hit a collider into that blueprint… then I will make that collider to do something when it get’s hit.

Right under Hit Actor is Hit Component. Send the Hit Component through as an input of your OnInteract function, and then just have a branch that checks to see if that component you passed through is the type of component you want to modify, and then run the function on that specific component

Oh… thanks. I was able to create a new Input where I accept the HitComponent thingie.
However… in the meantime I took my “Big Doors” outside the main actor so I’m working on them separately.

And I’m on another problem.

1.
181e4548b9240f2983bc99a6614ebb70dbb9adab.png

As you see. When I look at my doors, I Enable Input. If I press E, I will Open by doors. If I press E when the doors are already Opened, I will close the doors. Pretty cool.
My problem is that … when I look away the Enable Input still remains active, and if I press E again… I will trigger that event again.

I only want my events to happen when I press E when I look at** that **object.

2.
Is there a way to send variables info between blueprints?
For example. I have a BP … let’s call it “MainActor”. When I get into a collider in that “Main Actor” I want to check of DOOR 1 is still open. If it is open, I want to close it.
Also, I have another variable in the door that checks if the door can be opened or not. This MainActor after some events, I want to “unlock” the Door 2.

How can I send variables between actors?

Thanks

Think you’re overcomplicating things a bit. You don’t want to be enabling and disabling input like that.

Break it down…

So…the starting point here is ‘when the player presses E’. When that happens, do a raycast, check what you hit. If the thing you hit is something you can interact with, then call the Interact function.

There are a number of different ways to do this, you could use Inheritance, Components, or Interfaces, neither way is ‘correct’.

Inheritance - Create an AActor called InteractibleActor. When the raycast hits it, check if the hit actor is of type InteractibleActor.

Components - Create an InteractibleComponent. When the ray cast hits an actor, check if it has an InteractibleComponent (or check if you’ve hit the InteractibleComponent)

Interfaces - Create an interface called InteractibleInterface. When the ray cast hits an actor, check if it implements InteractibleInterface.

I already have an interface… so maybe I can do it like that? Problem is that there are a lot of unknowns for me right now and my head is a bit of messed up right now with all this new info coming in :smiley:

40dd463e0ba4d480a9efb28ab991335a04312e97.png

This is my current Raycast. You are suggesting to replace the “Event Tick” to “press E”. My press “E” is actually an InputAction InteractWithObject. I did this in the project settings > Input.

EDIT:
Ok… this works… so far. I also am able to check for variables & send events to other BP by using Cast To:
889ac0a8be762698a5f9ae393432030ed7334ed1.png


Thanks for the help so far!! <3

Well, where you put the raycast is up to you.

If you want to change the cursor or have some other indicator tell the player they are looking at an interactible object, then you need to do it in the Tick.

Otherwise, if you don’t care what the player is looking at until they press ‘E’ (or InteractWithObject, whatever InputAction you’ve set up), then you don’t need to raycast every frame.

Yea well… the main idea was to show the user that an object is interactible when he looks at an object for X seconds. Like after X seconds display some text.
But dunno now. Everything is way to messy in my head on how to do some stuff… and other stuff start piling up.

Like I created a sequencer animation that was animating my door (BP door). I duplicated the door with the idea of that new door using the same logic, but it turns out that the Sequencer animation is “hardcoted” to my first door. So when I interact with the 2nd Door, my 1st Door opens. Now I need to find a way on how to do this the “proper way”.

So in a way I would still like to learn on how to do that proper “Every Tick” raycast… and me knowing when I look and stop looking at that object.

Hi,

You could try the following:
-Create a boolean called ‘bLookingAtInteractable’ and an actor reference variable called ‘LastLookedAtInteractable’.

-Make a function called ‘LookAtTimer’.

-Make a function called ‘TraceForInteractable’ that does the line trace and returns a ‘true’ bool if the trace hit an object with the interface and also returns a reference to that hit actor.

-When ‘E’ is pressed, call the ‘TraceForInteractable’ function, and if it returned true, set the ‘bLookingAtInteractable’ to true, and assign the returned actor to the ‘LastLookedAtInteractable’ variable. After this, start a timer by function name. The function you want to call is ‘LookAtTimer’ (set the timer according to how much you want to wait before the text appears).

-In your TickEvent, create a branch and only proceed if the ‘bLookingAtInteractable’ is true. Call the ‘TraceForInteractable’ each frame, and check if the trace hit anything. If it did, compare the hit actor to your stored ‘LastLookedAtInteractable’ actor. If they are not equal (the Player is not looking at the same object that he was looking at when he pressed ‘E’), clear the timer by function name (LookAtTimer), and set the ‘bLookingAtInteractable’ to false. Also do this if the trace didn’t hit anything.

-Lat step is to define the ‘LookAtTimer’ function: to double check, make a branch that checks if the ‘bLookingAtInteractable’ is true, and then make the logic you want to happen there (like show the name of the interactable object on the hud etc.)

One thing to remember is to prepare for the case when the Player presses ‘E’ again while he is still looking at the same object. If you want your timer to restart then, make the clear ‘LookAtTimer’ the first step when the ‘E’ event is executed.