Closest Socket to Mouse location

Hi all,

Thanks for your time in reviewing this question.

I have coded a blueprint that once activated tries to calculate the distance between the mouse and all the given sockets, then returns the closest one.

I cannot get the mouse and sockets to correctly calculate. I have tried different calculates, but it seems that the location of the mouse is being wrongly reported.

Below is the blueprint as it stands:


However I have tried all the following :

Using standard vectors, using Get mouse position, get mouse position scaled by DPI, changing the method of calculation to Hypotenuse but no matter what, it always reports the furthest left most socket as the closest to the mouse! I have checked and the socket locations are being return correctly, it seems the mouse position is offset so far left that I cannot get it to be accurate, anyone have any ideas?


Do you want to get distance in screen space or in world space?

There is “Small” problem with interface and rendered image. Unreal kind of has 3 different coordinate systems, one is 3d world, second hud coordiantes and 3rd is touch/mouse coordinates.

You can get quite accurate location of raytrace below mouse pointer, but that will not work for sockets on 3d mesh. Well will not work right from start, you need to check results in some way. And i have idea for it:

get vectors to hit under mouse location, and all vectors to your sockets as visible from camera (same way raytrace for mouse works).
To get them substract world camera location and world socket location.

Now normalize raytrace vector and vector to socket. And calculate dot product (its cosinus of angle between them). IF that Dot product is more than for eg. 0.99 you almost hit socket. You can make second check for distance if you have more than one socket near that line.

Thank Nawrot, I will give that a go and see if I can make that work. Seems very convoluted for something I first imagined would be easy!!

Either / OR

I am trying to work out which socket the mouse at any given time is closest to. So as long as they both report the location in the space it should be a matter of calculating the distance between and comparing that distance to a previously known number.

The difference between your cursor in world space and the location of your pawn would be essentially the same. So, I think you would need to project the socket’s location to screen space, and calculate it there for it to make sense, if i’m understanding what you want. (Cursor location and socket location in screen space).
Here is how you could do it, I’m using an array of 5 socket names that I added to my skeleton, but you can change that to how you like to do it, though it will need to be an array for this to work. Either way, this will get you the shortest distance, and also the index of the socket in SocketNames.



edit: by the way, I did that in my playercontroller. Also, just realized you don’t need to store the distances, updated the graph.

You sir, are a goddam Genius.

Thank you so much, you have just helped me complete a task I had almost written off.

I wish there was a better way I could repay you other than a sincere, and honestly meant, Thank you.

Ha, no problem :slight_smile:

Now I have a lot of my game working I have come across a different issue. Getting this to work in multiplayer!!!

When I select an object, and have it move around my character as shown above, it works in Client or Multicast mode, but acts as if its an instanced action and the other players do not see anything, the weapon stays at its original location and whats worse others can then take it and it duplicates!

When I run the code on the server, none including the owning client sees the weapon move.

I have made sure the item being moved is replicated properly, and also check the box for replicate movement, but nothing is happening eitherway… any hints?


Actually it was pretty simple, I just had to break the code up into 2 parts, the first part being on the server and the 2nd part multicast. All I then had to do was pass parameters as replicated variables seemed unreliable.

Anyway, it almost works flawlessly now, Just need to stop custom depth from being shown on all clients…

Thank you for your great answer :). I have a question. How did you implement the dynamically moving rectangle? Can you please teach me in detail? I think you implemented it with draw Rect function of HUD… but I don’t know how to make the rectangle moving dynamically.

Hey, I am pretty sure I did that using the “Draw Debug Point” node. It takes a vector input and draws a rectangle at the position. As the name suggests, that would really only be used for debug/testing purposes and it isn’t something you would include in your finished game.

Thank you for your kind answer :slight_smile: Have a good day!