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.
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.
answered Aug 05 '15 at 06:16 PM
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.
answered Aug 06 '15 at 02:28 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here