Download

Animate a Crosshair Based on Direction of Mouse

I’m trying to have a crosshair that changes depending on which way you swing your mouse after left clicking. For example, you left click and then drag the mouse to the right, the right part of the crosshair will change to indicate that the mouse is going right.

What I’ve done to achieve this is I made a function that calculates the direction of my mouse based on the axis inputs. It checks if the inputs are greater or less than certain values and then sets a Name variable to Up, Down, Left, Right, etc.

Then I had my HUD draw lines based on the Name variable (switch on Name). So if the variable was set to Right, it would set the translation and color of all lines (via Sequence Node) except the “Right” one to 0 and white, while the “Right” line was translated and colored green to indicate the direction.

Is this the best way to do this? This is just what I figured out after a while of brainstorming it and I want to know if there’s a better way to do this.

Sounds about right to me. Though you shouldn’t need to reset all four lines every frame, only the two that change (one turning on, one turning off).

As for one turning on and one turning off, do you know how this could be done?

The problem I’m running into with that is that you have to know the previous line changed in order to do it. So if you went Right and then Down, it would need to know that you previously went Right.

Hmm, actually what I said may be total baloney. You’re going to be checking it every frame anyway, it probably makes just as much sense to set all four of them than it does to check if it’s changed and keep track of which one was active last, etc.

If you do want to keep track, just make two reference variables, one for the active line this frame, one for the active line last frame (say A(current) and B(last)). If A and B are the same, do nothing. If they’re different, set B inactive, set A active, and set B to equal A (so they’ll be the same next frame unless A changes again).

For turning them on and off, presumably you’ll have two little functions to change the color and translation. I would just make a unit vector2d from your mouse direction (so (1,0) is right, (0,-1) is up, since UMG measures Y down from the top, etc.). Then you can just multiply that by whatever offset amount you want and add it to the line’s position.

edit: Nvm I understand what you’re saying now.

8104c224bc4f45fdfdc54584139d410db2711efd.jpeg

Would you have done this differently?