- Home /
Question by
ProtocolUnknown · Sep 03, 2020 at 03:00 AM ·
rotationmobilecontrols
Rotating object using touch controls mobile
I am making this game for iOS mobile. I want to make it so that the player can rotate the ship on its z-axis by touching that white dot and rotating it by dragging, and the ship will follow the same rotation. I want to make it so that the dot remains hinged to the circle outline so that it moves only on the circumference of the circle. The tip of the ship will always point towards the dot. Any help would be appreciated.
This is my code so far:
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos))
{
switch (touch.phase)
{
case TouchPhase.Began:
startingPositionX = touch.position.x;
startingPositionY = touch.position.y;
break;
case TouchPhase.Moved:
float angle = Mathf.Atan2(startingPositionX - Input.GetTouch(0).position.x,
2 * startingPositionY - Input.GetTouch(0).position.y) * Mathf.Rad2Deg;
ship.transform.Rotate(0f, 0f, angle);
Debug.Log("angle " + angle);
break;
case TouchPhase.Ended:
Debug.Log("Touch Phase Ended.");
break;
}
}
}
}
annotation-2020-09-02-225233.png
(13.3 kB)
Comment