Trying to get things working so that the player can use circular mouse movements to control the turning of a wheel in a game. As an example, how turning the wheel works in Amnesia: The Dark Descent.
It’s semi-working in as far as I think I have been able to determine the magnitude of the player moving the mouse. I take the input each frame by using Mouse.current.delta.value and grab the X and Y values. Then I can compare the current values to the values from the last frame and do this to get the magnitude:
private double GetDistance(double x1, double y1, double x2, double y2)
{
return Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
}
Where x2 and y2 are the current frame and x1 and y1 are the previous frame. I might be running into a problem with trying to determine if the mouse is moving clockwise or counter-clockwise by checking the current position plus the last TWO positions:
private RotateDirection GetDirection(double latestX, double middleX, double oldestX, double latestY, double middleY, double oldestY)
{
var val = (middleY - oldestY) * (latestX - middleX) - (middleX - oldestX) * (latestY - middleY);
Debug.Log($"val: {val}");
if(Math.Abs(val) < 15)
return RotateDirection.None;
if (val > 0)
return RotateDirection.Forward;
return RotateDirection.Backward;
}
I have the absolute value check in case the mouse movements are very small, although this might be completely unnecessary. When I test it, the wheel is turning but it switches between Forward and Backward rotation rapidly, so it’s definitely not consistent. To actually rotate the wheel I’m just using a switch to test for forward and backward:
case RotateDirection.Forward:
transform.Rotate(Vector3.forward, (float)result.Magnitude * Time.deltaTime, Space.Self);
break;
case RotateDirection.Backward:
transform.Rotate(-Vector3.forward, (float)result.Magnitude * Time.deltaTime, Space.Self);
break;
I’m wondering if my method of getting the mouse position is completely wrong. I am using a Locked cursor so the mouse position doesn’t change, but I am checking the delta as shown above. This might be incorrect and causing erroneous values to be read as the current X and Y values of the mouse input?
Thanks.