- Home /
Advance Unity 2d apply circle Rotation-z with mouse
Sorry I'm new to this, I am trying to rotate a circle from A to B with mouse.position, but I would like that the next time I receive a new mouse.position the circle rotates in z from the last position (B) to C, can someone help me ? Thanks current code:
Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
Vector2 mouseOnScreen = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
angle = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
//apply Rotation
transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle));
float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
{
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
}
CAn you explain what "next time I receive a new mouse.position the circle rotates in z" means?
In Z what? give reference
Sorry i didn't explain well, so i'm trying to rotate the circle in transform.rotation.z, and i can with this code, the problem is that when i rotate the circle again(click with mouse again) it directs the object to the mouse position (recognizes the mouse position as direction to start position to the next position) ins$$anonymous$$d of rotating from last position to the mouse position.
here is the video: https://takeafile.com/?f=rixukecagi
Answer by tormentoarmagedoom · Mar 06, 2020 at 02:08 PM
Hello.
You just need to apply your last rotation every time. So every time you rotate, need to store its final rotation for the next time:
transform.rotation = LastRotation + Quaternion.Euler(new Vector3(0f, 0f, angle));
LastRotation = transform.rotation;
Bye !
$$anonymous$$y answer was not code, was just n "structure. You need to SU$$anonymous$$ Euler angles!
LastRotation = transform.eulerAngles
It works!! thanks a lot!!
transform.rotation = Quaternion.AngleAxis(angle - startAngle, Vector3.forward);
I think you meant to multiply the quaternions, but the order would be off for that too