0
\$\begingroup\$

I wrote sample application, that allows to "rotate" object by mouse. Rotation occured by rotating of camera object around central object and the code is following:

float mouseX = -Input.GetAxis("Mouse X");
float mouseY = -Input.GetAxis("Mouse Y");

float magnitude = transform.position.magnitude;

Vector3 mouseSwipe = new Vector3(mouseX, mouseY, 0);
Vector3 startPoint = new Vector3((float)camera.pixelWidth / 2, (float)camera.pixelHeight / 2, magnitude - 1);

Vector3 startPointWorld = camera.ScreenToViewportPoint(startPoint);
Vector3 endPointWord = camera.ScreenToViewportPoint(startPoint + mouseSwipe);

Vector3 mouseSwipeWord = endPointWord - startPointWorld;

float dragLat = mouseSwipeWord.y;
float dragLng = mouseSwipeWord.x;

Vector3 oldPosition = transform.position / magnitude;

float lat = Mathf.Asin(oldPosition.y);
float rsmall = Mathf.Acos(oldPosition.y);

float lng = Mathf.Atan2(oldPosition.z / rsmall, oldPosition.x / rsmall);

lat += dragLat * 10 * 2 * Mathf.PI;
if( lat*180/Mathf.PI > 80 )
{
    lat = 80 * Mathf.PI / 180;
}
else if( lat*180/Mathf.PI < -80)
{
    lat = -80 * Mathf.PI / 180;
}

lng += dragLng * 10 * 2 * Mathf.PI * 2;

float y = Mathf.Sin(lat);
rsmall = Mathf.Cos(lat);

float x = rsmall * Mathf.Cos(lng);
float z = rsmall * Mathf.Sin(lng);

Vector3 newPosition = new Vector3(x, y, z);
newPosition *= magnitude;

transform.position = newPosition;

LookAtTarget();

The code assumes that mouse returns delta value on each swipe and this works well on Windows. For example, if I do several separate swipes, I can gradually rotate object for full circle.

Unfortunately, when I run this on Android, it doesn't happen. Each swipe object restarts from some position, depending on swipe point. I.e. on Android, swipes return in absolute value.

Is it possible to know, whether swipes retunrs delta or absolute values and how? Is it possible to claim compatible return units?

\$\endgroup\$
0
\$\begingroup\$

Have you checked what the value of Cursor.lockState?

My guess is that the mouse is locked to the center of the screen on Windows, but you can’t lock a user’s finger in place very easily.

https://docs.unity3d.com/ScriptReference/Cursor-lockState.html

\$\endgroup\$
  • \$\begingroup\$ Cursor.lockState == None in my case \$\endgroup\$ – Dims Jun 17 '18 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.