- Home /
Problem is not reproducible or outdated
How do i rotate something on a new rotation axis?
It's hard to be specific but il try to explain my problem via picture
So i want to rotate the cylinder 45 degrees on the axis that its facing the ball object. (I don't want to rotate the cylinder to face the object and then rotate it 45 degrees) Axis is presented in magenta.
Answer by robertbu · May 25, 2013 at 06:03 PM
You get the axis using the cross product (Vector3.Cross()) between the Up Vector and the vector to the ball. So the code would be something like this (untested):
var v3 = ball.transform.position - transform.position;
var v3Axis = Vector3.Cross(v3, transform.up);
transform.RotateAround(transform.position, v3Axis, 45.0);
Note this assumes the script is attached to the cylinder. Also you may have to reverse the parameters in the Vector3.Cross() to get the rotation to go the correct direction.
Oh, this will fail if v3 and transform.up are the same. This is the situation where the ball is directly above or below the cylinder, so there is no "side" to identify. Since I don't know what you are doing, I cannot advise you on how to handle this case.
i think i got it with normalized direction vector and that vector3 cross function, yet i cannot possibly handle that rotateAround function since it behaves uncontrollable if the ball position changes. i used a few quaternion functions ins$$anonymous$$d. thanks!