How would I make it so objects have to be under my crosshair to interact with them?

Hey guys, I am trying to make it so I can only open doors and interact with other objects if my crosshair is looking at the object that I am attempting to interact with. As of now, all of my doors open with a trigger box and a key press, however, this allows me to still open the door if I am looking in the complete opposite direction. How would I make it so the door would have to be under my crosshair for the key press to successfully open it? Thanks.

I understand the door blueprint logic, but I can’t really figure out how to do the player blueprint. Could you take me through it step-by-step?

Hi Anaklosmos.

There are quite a few ways to accomplish that. Since you already have a system involving trigger boxes and key presses - the latter, I assume, implemented using the good old EnableInput-on-the-door pattern - maybe it would be useful to keep that as it is, and then just build something on top of that.

So, the player enters the box, at which point the door blueprint starts to accept input, then the player presses ‘E’ (or something) and the door opens, right?

The only thing we need then is to inject a little check after the ‘E’ input event, but before the actual opening, something like a function on either the PlayerController or the Character, whichever is convenient, that can tell the door whether it’s currently being looked at or not.

Let’s say we add a function called IsLookingAtMe to the Character. It should return a boolean: true if the character is indeed looking at the door, otherwise false.

So, once the door receives the input key event, it would call Character::IsLookingAtMe(self) passing itself as the argument, a normal Actor-type reference. The required character reference could be cast and stored when the triggerbox receives an overlap event.

The logic in the door blueprint would then simply be something like:

E.Pressed => CharacterRef.IsLookingAtMe(reference to self) => Branch-True => Open the door or if Branch-False=> Do nothing.

The other half of the solution is to implement the IsLookingAtMe(actorRef) function in your character BP. It would simply do a linetrace from the character’s camera component and straight ahead for some distance along the forward vector of said camera component. If the linetrace detects a hit, the hit actor would be compared to the actor reference that was passed into the function, and if it is the same, the function returns true. Otherwise false.

And… that would be it.

Sure… The function in your player character (I uses FirstPersonTemplate for this example) would look like below. The “TraceDistance” is just a float variable you add in the character BP. Set to 300 it would trace up to 3 meters in front of the player, for example. All the variables whose names begin with “Local” are, well, local variables added in the function only.

https://dl.dropboxusercontent.com/u/2888286/IsLookingAtMeFunc.png

I have successfully created the player function, but I am having one final problem. I cannot call the function correctly into the “Door” blueprint. Below I have attached both my Function Blueprint that is attached to my character and the blueprint of the other object that I am trying to interact with. (I was not using an actual door blueprint, but instead, this particle system creation blueprint.) I have to assume that the system will work for both objects, as the same functionality goes with both. Anyways, when I attempt to call this function into the particle system blueprint, it will not compile because I need an Object to plug into my Cast To FirstPersonCharacter? This node is the one circled below. How could I fix this?

Plug in a “self” reference or a “get player character” node.

what i do is i make a line trace after i press “e”. if it collides, the line trace will output true and you can go from there.

In your “Door” blueprint, the logic following the keypress could look like this:

https://dl.dropboxusercontent.com/u/2888286/IsLookingAtMeFunc2.png

Three IMPORTANT notes here:

  1. This will only work in a single-player scenario. If you’re doing multiplayer, it needs to be a little bit more refined.
  2. Note that the “Door” must pass a reference to itself in the call to IsLookingAtMe, otherwise the linetrace has nothing to compare the hit to, and the function would always return False.
  3. Also note that the “Door” must have something that the linetrace can actually hit, i.e. something that will end up blocking the trace. A particle system does not have anything like that by default, whereas an actual door (or any visible geometry) would in the form of a mesh. So while this setup does indeed work with any target, one requirement is that it can be hit by a visibility trace. So if your use case really is a particle system, you need to add some collider to the blueprint containing the particles, otherwise it won’t work.

Thank you so much for the help! I got it to work perfectly!