3
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ We are not a code writing service. We only solve coding problems. \$\endgroup\$ – Bálint Nov 14 '16 at 10:52
  • \$\begingroup\$ 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; \$\endgroup\$ – 3Dave Nov 14 '16 at 14:00
2
\$\begingroup\$

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)
}
\$\endgroup\$
  • \$\begingroup\$ thanks - But that doesnt use the anchor as pivot right? \$\endgroup\$ – OC_RaizW Nov 15 '16 at 9:55
  • \$\begingroup\$ @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) \$\endgroup\$ – Foggzie Nov 15 '16 at 16:58
  • \$\begingroup\$ Yes, thats an error. transform.RotateAround() is what I wanted to use. I edited the Main Question. \$\endgroup\$ – OC_RaizW Nov 17 '16 at 9:29
  • \$\begingroup\$ 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 \$\endgroup\$ – OC_RaizW Nov 17 '16 at 9:36
-1
\$\begingroup\$

Maybe this can help you:

After the rotating of parent:

foreach (Transform child in transform) {
  child.rotation = // reset value
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.