- Home /
How to rotate an object around a screen point (mousePosition)?
Hello, this is pretty simple. In my NGUI inventory system, you could rotate your items around the mouse cursor (I'm currently doing it in an obscured way). However, since I'm using NGUI, the texture is attached to a gameObject, meaning I could rotate the texture if I could just rotate the gameObject its attached to.
I want to be able to rotate around the cursor:
I've tried a lot of stuff, nothing seems to be working :( I tried this script, doing it this way is out of question for me. I tried something like:
static public void RotateWidgetAround(UIWidget widget, Vector3 aroundPoint, Vector3 aroundAxis, float angle)
{
widget.transform.RotateAround(aroundPoint, aroundAxis, angle);
}
and calling it like:
RotateWidgetAround(itemTexture, Input.mousePosition, Vector3.forward, (rotDir == RotationDirection.Clockwise) ? -90 : 90);
and:
RotateWidgetAround(itemTexture, mainCamera.ScreenToWorldPoint(Input.mousePosition), Vector3.forward, (rotDir == RotationDirection.Clockwise) ? -90 : 90);
Any ideas? - Thanks for your help.
You don't provide enough code to figure out a good solution, but I do see one problem. For ScreenToWorldPoint() to work, the 'Z' parameter needs to be the positive distance in front of the camera to project the point. By default 'Z' is 0, and therefore the calculation will always return the camera position.
Answer by vexe · Aug 23, 2013 at 05:59 AM
It was as simple as grouping the rect (the green rect you see ^) and item (the m16 ^) textures into one parent object, and apply the rotation on the parent, very simple:
parent.Rotate(Vector3.forward, (rotDir == RotationDirection.Clockwise) ? -90 : 90);
This works because the parent is already following the mouse cursor. Who would have thought?
Your answer
![](https://koobas.hobune.stream/wayback/20220613112807im_/https://answers.unity.com/themes/thub/images/avi.jpg)