# Unity / Rotate a point around an arbitrary axis and anchor

I'm trying to find out the point which is already rotated. To solve this I did a Rotation, save the position, set the previous position and return my position, like this:

Vector3 rotatePointAroundAxis(float angle ,Vector3 axis, Vector3 anchor)
{
Vector3 oldPosition = gameobject.transform.position;
gameobject.transform.RotateAround(gameobject.GetComponent<HingeJoint>().anchor, gameobject.GetComponent<HingeJoint>().axis, angle);

Vector3 newPosition = gameobject.transform.position;
gameobject.transform.position = oldPosition;
return newPosition;
}


I searched already. And I found very hard solutions. http://inside.mines.edu/fs_home/gmurray/ArbitraryAxisRotation/ - by the way i dont think I understand this, it seems like too much.

Is there any way to change that function to a "simple" one, so I don't need to rotate things forth and back to get the coordinates?

EDIT:

I believe tranform.RotateAround() takes the position and rotation from the transform parameters and calculates the solution and changes the current position and rotation of the gameobject.

So the solution should take 5 parameters:

1. current position of the unrotated Object
2. current rotation of the unrotated Object
3. the point the Object should be able to pass
4. the rotation axis
5. the angle

and return 2 parameters:

1. the rotated objects new rotation
2. the rotated objects new position
• We are not a code writing service. We only solve coding problems. Nov 14, 2016 at 10:52
• Create a new Transform object, copied from gameObject.transform. Use that for everything else you're doing, so that it won'teffect your parent root /gameobject. Transform xform = gameobject.transform; Nov 14, 2016 at 14:00

The concept of a "rotation" is represented by a Quaternion. These are built into Unity and there's no need for you to understand the math in that link you posted.

To create a rotation from an angle & axis, use Quaternion.AngleAxis. Once you have it, you can simply multiply it by the Vector3 you're trying to rotate. Your function would look like this:

Vector3 rotatePointAroundAxis(Vector3 point, float angle, Vector3 axis)
{
Quaternion q = Quaternion.AngleAxis(angle, axis);
return q * point; //Note: q must be first (point * q wouldn't compile)
}

• thanks - But that doesnt use the anchor as pivot right? Nov 15, 2016 at 9:55
• @OC_RaizW No, it only uses the axis. If you're trying to rotate around an axis, you don't need a pivot point. Also, I'm not sure how your provided code compiles; there is no Rotate() function on Transform that takes (Vector3, float, Vector3, Vector3) Nov 15, 2016 at 16:58
• Yes, thats an error. transform.RotateAround() is what I wanted to use. I edited the Main Question. Nov 17, 2016 at 9:29
• tranform.RotateAround() takes the position and rotation of the Gameobject itself (i think). So the solution to the answer could take 5 parameters: 1. current position of the unrotated Object 2. current rotation of the unrotated Object 3. the point the Object should be able to pass 4. the rotation axis 5. the angle and return 2 parameters: 1. the rotated objects new rotation 2. the rotated objects new position Nov 17, 2016 at 9:36

foreach (Transform child in transform) {