• News

• Industries

• Learning & Support

• Community

• Marketplace

# Mouse dragging direction

Hello everyone,
does anyone know how can I detect the direction of a mouse input? Basically, when the player clicks the right mouse button and drags the mouse (whilst holding the mouse button), how can I tell the direction he is inputting? E.g. up-down, down-up, left-right, northeast-southwest, etc.

You can use IE_Pressed to store the mouse position and start a “hold status” and then use IE_Released to know the end of the “hold status”… Then compare the stored position with the current mouse position and you will get the drag direction.

@Tabriz
Thank you very much for your answer. But how do i compare the 2 stored values to get the direction?

Would something like this math operation work?
let’s say i have to 2DVector variables: v1 and v2
v1: stores the value when the mouse button is pressed
v2: stored the value when sometime has passed and the mouse button is released

and then to get the direction i make this:

note* i didn’t write it the way it should be in ue4 i suposse. Please bear with me as i’m learning it yet.

Yep, FMath::Atan2 is a good way.

ok thank you once again Tabriz.
So if this formula works for the direction in a 2d space.
Would this one work to get the direction in a 3d space?

This time the vectors variables should be 3D instead of 2D
Vector1: stores the value when the mouse button is pressed
Vector2: stores the value sometime after and when the mouse button is released

Will zdirection get the correct direction from Vector1 to Vector2 in the scene?

Thank you very much for your time ^^

The 3D version is something like… ( Careful, the example is using the XYZ coords )

Or

Check the source file KismetMathLibrary.cpp… is a nice place.

Oh i see. Thank you very much Tabriz
I’ll take a look at that file too.

Or you could take the first vector, subtract the second, and then normalize it.

That’d give you a direction vector.

@Mesothere
Thank you very much didn’t thought on that. That’s an easy way to get a direction vector.