I struggle to create correct rotation of a circle. Its a mobile game, in the center of the screen is the circle and players can rotate it around Z axis by touch. It must rotate by touching any part of the screen. Ill appreciate your help with this one, hope here are some mobile gaming gurus)
Below is my half-working example:
void Update()
{
if (Input.touchCount > 0)
{
theTouch = Input.GetTouch(0);
if (theTouch.phase == TouchPhase.Began)
{
touchStartPosition = theTouch.position;
}
else if (theTouch.phase == TouchPhase.Moved || theTouch.phase == TouchPhase.Ended)
{
if (touchEndPosition == theTouch.position) return;
touchEndPosition = theTouch.position;
float x = touchEndPosition.x - touchStartPosition.x;
float y = touchEndPosition.y - touchStartPosition.y;
double distance = Math.Sqrt(x * x + y * y);
float f = Convert.ToSingle(distance);
f /= screenSensitivity;
Quaternion pos = circle.transform.rotation;
Vector2 endPoint = cam.ScreenToWorldPoint(new Vector3(touchEndPosition.x, touchEndPosition.y, cam.nearClipPlane));
Vector2 startPoint = cam.ScreenToWorldPoint(new Vector3(touchStartPosition.x, touchStartPosition.y, cam.nearClipPlane));
Vector3 circlePos = circle.transform.position;
if ((Mathf.Abs(y) > Mathf.Abs(x) || (Mathf.Abs(y) == Mathf.Abs(x))))
{
if (y < 0)
{
f *= -1;
}
if (endPoint.x > circlePos.x)
{
f *= -1;
}
f *= -1;
} else
{
if (x < 0)
{
f *= -1;
}
if (endPoint.y > circlePos.y)
{
f *= -1;
}
}
currentRotation += new Vector3(0, 0, circle.transform.rotation.z + f);
circle.transform.localEulerAngles = currentRotation;
touchStartPosition = touchEndPosition;
}
}
}
