HUD Crosshairs not matching Mouse OnClicked Event : A Dumb Problem in the scheme of the Universe.

I seem to get stuck in UE4 in the dumbest problems, something like an on clicked event taking up huge swathes of debugging time, so I apologize for this dumb thread in which
I seem to be struggling with a dumb problem.

I’m using a HUD crosshair in my game. However I want the interaction like you might get when you click with the mouse, so an object will change colors, or highlight, or make a sound, or some such things which are incredibly common to every game ever made. Thus dumb problem that should be a simple thing.

This isn’t working. My mouse location does not match my crosshairs, I have to pick up to the right, and I realize this is a scaling issue, but how can i lock the OnClicked to the actual cross hair instead? Why is this even an issue? Issues like this make me want to give up and become a buddhist monk, and work on things that are easier, like what is the meaning of life.

But I’m rambling now, because I typically get into my studio to work at 5:30 am on any given day of the week so that I can accrue enough hours to actually make progress on my game, and I totally get derailed by again, dumb problems like this, which I never had in Unity I want to point out hastily, but no I’m not going to Unity again, the motion makes me vomit.

Solutions: I know people throw out these answers like, “hey mate, use a LineTrace for that” which is a super reasonable response, in a possibly Australian accent which always makes people perk up a little and feel happier, because Aussies have a way of doing that to people,

HOWEVER : I can’t find the exact way to use a line trace, and even when i find samples everyone is feeding in the OnClicked (which uses the same mouse input) and thus I’m left with the same problem.

Now I’m going to do something desperate like meditate on this while I try to remind myself to not let these little things get to me… not good for my blood pressure.

Im not exactly sure what you’re trying to achieve here, is this clicking on a widget for UI, or is this an actor in the world?

A picture of your problem would be beneficial. Since you’re speaking of using line trace Im going to assume this is an actor in the world, and that you are trying to trace from the players perspective. Here is a picture of how I use a line trace to pick up a weapon.

Where the start location is the player characters camera world location, and the end location is the forward vector of the camera (which is generally down the X axis) multiplied by 500 (which is 5 meters, since the Get Actor Forward Vector returns a vector length of 1 unit) and then add that with the cameras location.

As a tip I would suggest setting Draw Debug Type to “For Duration” so you can see your line trace, and then set up a branch node after the line trace to check whether or not the hit was successful, and if it was, or wasn’t then use a print node to make sure. I hope this helps somewhat.

Hi Shirk,

This isn’t a UMG widget, i’d like to avoid the UMG for this action because like your weapon, I have a lot of objects that I want to interact with. I know I can make this a popup UMG if I have to, but this seems like it should be so straight forward.

For starters though, I am using the Hud_forPhysicsDemo as my HUD_CLASS : The reason being that I am interacting with most of my objects physically in my game, so physics objects.

I have a sound button icon (a static mesh) in my game, (will be many) and I would like my player to click on that object, to hear what it says. The reason it’s a static object is that for each puzzle, I have this static mesh, so i can replace the sound it makes easily, and use it as a blueprint.

Where it goes wrong? I am using line trace in my PlayerCharacter of course, in order to use the Physics HUD, it’s been fine. it uses the cross-hair on my screen just fine. However when I’m taking my static_mesh blueprint as described above, and try to get a result (OnClicked (PLAY_SOUND_MODEL))… the result is that it only plays if I angle up my screen, often with my crosshairs entirely missing the object in question.

The obvious reason is that the OnClicked is using the mouse input. Anyway, I have a few precious minutes left before my wife and i swap out morning ritual (she goes to her office to work and I take care of the kids)… we have a complicated home life, where we both work from home, and homeschool, thus my tiny morning window of 5:30-8:45 is almost over (full day tomorrow in here).

going to hook that up before she comes in and deflates my game development bubble…

okay i’m past my work window, but I did try this. I got stuck at the Object Types. How do i make my single BP_PLAY_SOUND object an array (do i want to? I’m not even sure exactly what that would mean).

i would post an image, but my young son is calling for me… i’ll try to post more, but if you have any more hints let me know.

thanks

Problem with unreal is that you get about 4 different orientations and scales when you fiddle with touch mouse and 3d to screen space problems.
I tackled this all with touch interface for mobiles. On top of all that your screeen resolution may change during gameplay (taskbar or rotation on mobiles).

Luckily now we4 are making PC game, so no more touch location to HUD location mess.

What i did for current game:
i get touch/click trace location under mouse. Id does not always work well (for eg when character goes under some object and hit location happens above character but a bit behind it also). This is simple solution.
To prevent above problem i project mouse location to a horizontal plane at [0,0,0], its simple trigonometry really. This is like mouse trace would collide with horizontal plane. We are making top down game so no edge cases of looking horizontally.

Then i added 3d umg widget to player pawn, and I always render it exactly (set its location) to mouse hit location in 3d space.
But i tell umg to render it in hud space not in 3d, this way it is always on top of all 3d geometry, and its always accurate to mouse click, because it is rendered at exactly mouse click location.

wow why is this so difficult. with all the amazing things you can do with this software it seems a ridiculous thing to burn um zillions of hours

to add insult to injury the trace distance as set in the playerController seems to make zero difference. I have to be right up against my object to make this work.

I’m still struggling with this, i’ve played with making my collider larger, off to the side to capture the mouse click, but i can’t actually see the mouse icon and believe it’s random every game play.

I realize i probably have to use the Check trace as used in the playerCharacter for physics games, but I’m already using this, not sure I can make another check trace from the LMB in each object I encounter. I’ve tried to plug this in directly to my LMB trace in PlayerCharacter but to no avail (I had to make my object a physics object with infinite weight so it wouldn’t move)

still working on this… will say if i find any solution.

I gave the boys a snack and sneaked off to my studio to work on this because I had some ideas. I somewhat exhausted hacks, like making the collider big, or offset to the side, because the mouse shifts all over, it is unpredictable.

So I went back to the trace solution, and again took a look at my PlayerCharacter (from the PhysicsDemo is where I initially got it). It’s not an ideal solution because I have to account for every one of these elements, but it works. I realized that the function I was trying to make do the trace, was in fact a function call, not the actual Trace Node. I have to get back to my sons, and will post images later, but the basic way i did this so far, was to Tag my object “PlaySound”.

Then in PlayerCharacter, I extended off of the BREAK HIT RESULT node’s Hit ACTOR, and assigned my PlaySound to an ACTOR HAS TAG function. This fed into the Return Node where I set a boolean for PlaySound.

I will show pictures later, I get about five minutes where I can literally try something without being needed. The bottom line though, although this works, is that I want to try to duplicate this in my object’s blueprint, not the player character, because it will be easier to stay organized if this check was done in the object, of which there will be very many, unless I can also get an object name hit off of my trace, then I may be able to make something more automated that accounts for every object without adding new nodes for every object.

In the case of the type of trace I used, which was LineTraceForObjects, it checks to see if it hits an object of the selected type. My objectType variable takes two inputs, both of which are the only things that will cause the trace to return true, and this way by using an object trace is to only get the objects you want.
TINiJqy.png

In order to specify a custom object, which Im assuming is what you want you have to go into the blueprint of the mesh you want to have the trace return true for, go to the collision section in the details panel and expand the Collision Presets and change Object Type to which ever you would like.
TINiJqy.png

If you want to add a custom object type you need to go into project settings, found at Edit>Project Settings>Engine>Collision and add your new custom Object type to the Object Channels. As you can see, both Items and Ammo object channels are custom that ive added myself, and I use the trace to find only these two object types.

I hope this helps with what youre doing, as all you have to now do is change the object type of each blueprint to a type you want the line trace to return true for. Dont forget to make a variable in the player blueprint that can store these, you can just drag off of the trace node and create an array variable that way.

Are you sure that the HUD crosshair texture is being rendered at the correct position and that it’s not offset?

yes the crosshair is definitely in the right position, the discrepancy is very large, and it changes - so that it isn’t something I can count on just doing an offset of some kind in my collision.

@Shirk, i’ve read through your notes, going to look at that closely again in a few minutes… trying to get my new laptop up and running before I go on vacation so i can take Unreal on the road while I’m away.

Turn on visible line debug for your traces, it is very easy to make mistakes in vector math.
Also on that “why it is all so complicated”, you either get flexible but complicated code, or you get easy but very specialized.
However bad thing is that documentation is very lacking in some areas.

True. I have this working using my PlayerCharacter, but I’m still trying to figure out how to make it work from within an object. I don’t think the engine likes this second call to LMB doing a trace when my PlayerCharacter already is. If there was a finite number of objects this might not be a problem, trying to think of an elegant solution.