I have a vector that I want to rotate so it points up at a 45 degree angle from its original direction. When I say "up" I mean so it is still traveling in the same direction except for on the y axis.
2 Answers
2http://answers.unity3d.com/questions/40582/rotate-a-vector3-direction
edit
To rotate around the local right vector you can calculate it like this:
{
// the Vector3 you want to rotate
var myVector : Vector3;
// help vector that points to the right
var right : Vector3 = Vector3.Cross(myVector,Vector3.up);
// rotate myVector around right
myVector = Quaternion.AngleAxis(45, right) * myVector;
[...]
I know how to rotate it about a world axis, or an axis obtained from a transform, but what about rotating it around its own "right" if it is considered to be "forward"
– anon23814272Do you mean that you don't have the corresponding transform available, but only the Vector3 that points "forward"? If this Vector3 lies in the x-z-plane (more precise it don't face straight up) you can calculate the "right" vector via cross-product. I can add an example
– Bunny83Maybe not the best way, and maybe it doesn't even work (can't try it out right now, rebuilding my pc). But I'd try something like:
Quaternion offset = Quaternion.Inverse(Quaternion.FromToRotation(myVec, Vector3.forward));
Quaternion rotateUp = Quaternion.FromToRotation(Vector3.forward, Vector3.forward + Vector3.up);
myVec = (offset * rotateUp) * myVec;
What is the question? How to do this trough code? How to do this in the editor? Please just read http://unity3d.com/support/documentation/Manual/Unity%20Basics.html and save your self a LOT of questions.
– anon73820239