- Home /
Rotate a vector
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.
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/$$anonymous$$anual/Unity%20Basics.html and save your self a LOT of questions.
Answer by Bunny83 · Apr 06, 2011 at 03:25 AM
http://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"
Do 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
Answer by groovecode · Apr 06, 2011 at 04:49 AM
Maybe 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;
Your answer
Follow this Question
Related Questions
c# modify only one axis of a quaternion 2 Answers
Unity's equivalent of Vector3.Transform(...) 2 Answers
How Make an Vector Follow an Quaternion? 1 Answer
Help with Quaternions and Vector3s 2 Answers