- Home /
Rotating an Object (Like Handle in Unity)
I want to rotate an object with the same affect as the Unity Rotation Handle.
The problem I am having is that when the object is flipped the rotation invert. I have managed to get the object to look at my mouse using Vector3.Dot and Planes but haven't found a way to check when to invert the rotation with the object.
I have looked into this script: http://answers.unity3d.com/questions/714203/rotate-object-via-script-like-in-editor.html but this rotate the object in world space. I would want to rotate it relative to the object.
I tried to change the values in the script above with this : http://answers.unity3d.com/questions/316918/local-forward.html
but then got a rotation that moved the object all over the place.
Thanks for the help!
Answer by BabilinApps · Nov 01, 2015 at 05:50 PM
You can do something like this to see if the object is flipped.
if (Vector3.Dot(transform.up, Vector3.down) > 0)
Then you can make your script look like this:
float _angle = 1;
if(Vector3.Dot(Item.transform.up,Vector3.down) < -.5f){
_angle *= -1;
} //this sees how flipped the object is.
Item.transform.Rotate(Camera.main.transform.right, Mathf.Deg2Rad * Input.GetAxis("Mouse Y") *
RotationSpeed * _angle);
I'm not %100 sure, it's been a long time sense I played with Vectors like that. Also it's untested code so please comment if you have a problem.
Answer by Tom01098 · Oct 31, 2015 at 12:44 PM
If you want to rotate it around an object, then you can parent it to that object and rotate that object
I would want the object to rotate with the $$anonymous$$ouse axes and in local space. The problem is when the X Axis becomes the Y. or if the object is turned so that the positive movement moves the object the opposite way.
Answer by starikcetin · Oct 31, 2015 at 11:22 PM
When the object gets flipped, duplicate angle with "-1". A sample of algorithm in 2D (x,y):
float angle = Vector2.Angle (Vector2.right, target-transform.position);
if ((target-transform.position).y < 0)
{
angle *= -1;
}
How would I do it in Vector 3? When I use your method I realized that when I debug the code it all returns as 90. How do I implement it into rotation. such as
Item.transform.Rotate(Camera.main.transform.right, $$anonymous$$athf.Deg2Rad * Input.GetAxis("$$anonymous$$ouse Y") *
RotationSpeed);
You have 3 angles and 3 axises right? Then do the thing for all the 3.