private void Update() {
Vector2 center = new Vector2(Screen.width / 2, Screen.height / 2);
float deltaY = Input.mousePosition.y - center.y;
float deltaX = Input.mousePosition.x - center.x;
float angle = Mathf.Atan2(deltaY, deltaX) * (180 / Mathf.PI);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, angle);
}
I have this script attached to a crank object that I would like to rotate by finding the angle between the mouse and the center of the screen. It works well enough, but I can’t find a way to calculate my total accumulated angle (Like if I rotated it around twice, it would report a total angle of 720 degrees). How would I go about doing this?
Vector2 center = new Vector2(Screen.width / 2, Screen.height / 2);
float deltaY = Input.mousePosition.y - center.y;
float deltaX = Input.mousePosition.x - center.x;
oldAngle = newAngle;
newAngle = Mathf.Atan2(deltaY, deltaX) * (180 / Mathf.PI);
//if (newAngle < 0) newAngle += 360;
float deltaAngle = Mathf.DeltaAngle(oldAngle, newAngle);
return deltaAngle;
Just used Mathf.DeltaAngle to find the closest difference between the newAngle and oldAngle (oldAngle is just newAngle from last frame) and added the deltaAngle to my total angle.