# rotate a hand crank with circular mouse motion?

hello.
I have here a little problem which I cannot solve.
I have not really a good idea how I could achieve this in my game.

my Game should be a 3D first person horror game.

basically it should work like this.

I want that the mouse controls for the player are deactivated and activated for the crank so that I can control the crank. that would no problem.

but I want that the player needs to rotate his mouse in a circular motion to rotate the crank to wind someting up or open a door.
like in this picture.

with one axis it would be no problem because I can map that to a float. but like in the middle picture a circular motion needs X and Y …

the crank on the right picture should only show how the crank could look like in my game.

what I already got coded was not that good. it was a empty that moves in its own vector2 and it is clamped to a distance to its parent. so I thought I could then just model a crank and let the Z axis look at the empty and then it would rotate with my mouse… but if I would rotate the mouse in a very small circle or would move the mouse from left to right it would instantly snap to the other side or it would rotate way too fast… so that idea is not good…

the way i would do this

create a mesh which is just a circular plane in your modeling program so you got a circle

tahts going to be your collider mesh

create an empty game object with that mesh collider attached and center it on your crank. it should basically be a circle the outside edge of which the crank handle would trace in movement. Got that?

Good

now your going to take the mouse position in word space and place it on the same plane as this collider of course. Since the mouse position is only 2d on the screen you get the third dimension (in this case the Z depth) by making it equal to the collider object Z.

Now you are going to do a raycast from the mouse position to the center of this mesh collider object.

that will give you info about the point on the edge of the collider closest to the mouse

with that info you now know what you need to know

you move the crank handle from current position to the closest point on bounds

if you like you can do this through a simple rotation around the Z axis

the angle to rotate is the angle between A & B where

A = crankknobpiece.transform.position - crank center point.transform.position

basiscally your going to need to know 2 things where the handle part of the crank is and where the actual shaft it rotates part is

B = nearest point on bounds so the raycasthit.point - crank center point

now you have

mouse
-
-
-
handle—crank center

and you can use vector3.angle(A,B)
to find the angle between mouse and handle from crank center point

then you rotate around the z axis angle degrees and arrive

YAY