x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.8
Tags:
more ▼

asked Aug 05 '15 at 05:52 PM in Blueprint Scripting

avatar image

Anaklosmos
170 35 29 54

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Aug 05 '15 at 06:16 PM

avatar image

Xenome
2.1k 64 54 120

avatar image Anaklosmos Aug 05 '15 at 06:31 PM

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?

avatar image Xenome Aug 05 '15 at 07:30 PM

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.

alt text

avatar image Anaklosmos Aug 05 '15 at 10:05 PM

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?

alt text

alt text

avatar image SkeetonYu Aug 06 '15 at 12:51 AM

Plug in a "self" reference or a "get player character" node.

avatar image Xenome Aug 06 '15 at 08:32 AM

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

alt text

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.

avatar image Anaklosmos Aug 06 '15 at 01:27 PM

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

(comments are locked)
10|2000 characters needed characters left

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.

more ▼

answered Aug 06 '15 at 02:28 AM

avatar image

alx0427
168 38 45 61

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question