hello. I’m a Unity Super Newbie.
After several days of searching and experimenting with relevant keywords, I kept getting the same problem, frustrated, and found the Unity forum! Any advice would be appreciated.
I set up the quad so that the main camera can see it, and then I try to rotate the quad like a pinwheel.
The basis of rotation is the center of the quad. Originally, I wanted to use the upper left corner of the quad as the pivot, but it was too difficult, so I am trying from the quad center pivot.
There is a small quad in the lower right corner of the Quad, which acts as a widget for dragging. My current goal is to click and drag this to rotate it by the angle it moved.
![]()
I wrote the code like the one below, but when I run it, the quad does not rotate smoothly to the obtained angle, but turns hard like a machine. The direction the mouse arrived after dragging and the direction the widget quad arrived after rotation are also different.
How can I modify the code so that the quad rotates by the angle moved by the drag?
IEnumerator OnMouseDown()
{
Vector3 screenCoord = Camera.main.WorldToScreenPoint(transform.position);
Vector3 mousePreCoord = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenCoord.z);
mousePreCoord = Camera.main.ScreenToWorldPoint(mousePreCoord) - handleObject.transform.position;
//Vector from the center of the quad to the previous mouse position in world coordinates
while (Input.GetMouseButton(0))
{
Vector3 mouseNowCoord = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenCoord.z);
mouseNowCoord = Camera.main.ScreenToWorldPoint(mouseNowCoord) - handleObject.transform.position;
//Vector from the center of the quad to the current mouse position in world coordinates
//Returns the angle between two vectors in the range 180 to -180.
//When viewed from the reference vector, mousePreCoord,
//if the mouseNowCoord vector is above, 0 to 180, if the mouseNowCoord vector is below, 0 to -180
float angle = Mathf.Atan2(mouseNowCoord.y - mousePreCoord.y, mouseNowCoord.x - mousePreCoord.x) * Mathf.Rad2Deg;
print($"angle:{angle} // handleObject.transform.eulerAngles:{handleObject.transform.eulerAngles}");
handleObject.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
mousePreCoord = mouseNowCoord;
yield return new WaitForSeconds(0.5f); //'yield return null' didn't work
}
}
