Your calculation of targetRotation is finding a point in the world that probably is not in the plane that the crank can reach. Imagine a plane that is perpendicular to the axis of the crank, with the crank handle at the center of it. Now imagine the line through the screen that is behind the mouse. You want the intersection point of the handle’s plane and the mouse’s line. Currently you clamp the mouse world X to zero instead of finding that intersection.
Thank you so much, you got me to the right path.
I’ve removed the clamp of mouseWorld.x and changed the upwards parameter of lookRotation from Vector3.left to look straight into the camera. Here’s the right code:
// speed of the rotation
float rotationSpeed = 4.0f;
// distance of the crank from the camera
float dist = Vector3.Distance(transform.position, Camera.main.transform.position);
// the position of the mouse
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
// convert the position of the mouse from screen to world point
Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(mousePos);
// the upwards of the rotation, it is the vector going from the crank position to the camera
Vector3 upwards = transform.position - Camera.main.transform.position;
// rotate the crank to look at the mouse
Quaternion targetRotation = Quaternion.LookRotation(mouseWorld - transform.position, upwards);
// smooth the rotation
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);