Getting mouse movement direction

So i want to replicate a kind of mouse control scheme mount and blade/war of the roses use, i.e. you click LMB and while dragging in a certain direction to get a result, but being an artist(animator) and not a programmer i find it rather difficult to wrap my head around it. I presume i need to store mouse position over 2 or 3 ticks and then build a vector from these 2 dots, and then somehow calculate its angle compared to some other vector, but how to do this exactly is beyond me. Help!

I want to help you, but i don’t know the control scheme of those two games.

You can get some information about your mouse from your controller.
Like “ButtonJustPressed” or “ButtonHoldDown” (don’t know the exact phrase right now).

So you could check per tick if the player pressed the left Mousebutton and store position by getting the mouse position and saving it in a vector2D. Now with the second Node about “ButtonHoldDown” you can update the current mouse position to a second variable. If the player now lets go of the mousebutton you will have the first click position and the last hold position in 2 vector2D’s.

Is there something more you want? I guess some Vector Math lessons would be nice for you, something simple like getting directen from one Vector to another or the length or something. If you need something more to be discussed please tell me.

If something is unclear i can start my UE4 and show you an example in the blueprint nodes.

PS: If you want the vector from point A to B you need to subtract A from B. (Endpoint - Startpoint = Direction from Start to End). Since you have 2 Vectors from my solution, you can do this and get the direction you dragged the mouse.

2 Likes

That actually helped me quite a bit, thanks.
I have another question now, Given that i have this vector from mouse position, how would you suggest i get the angle of it compared to, say, a vector that points up. Or well, essentially i need a kind of circle thats split up into different segments so i can check if the angle of the mouse direction belongs to one of them

This should probably be it’s own question but you can get the angle by using Math::Atan2(). This function returns the angle in a circle at which the vector you pass in points to. The return value is probably in radians, I haven’t tested it in UE4.

float angle = FMath::Atan2(MouseCurrentPosition.Y - MouseStartDragPosition.Y, MouseCurrentPosition.X - MouseStartDragPosition.X);

Since you want the center of the circle to be the position where you first pressed the button you need to subtract that point from the current position of the mouse, like eXi has explained. This function also takes the Y position as the first parameter.