How to rotate a Vector3?

How do I rotate a Vector3 (not a Transform) so its Y direction looks towards Vector3.up?

In other words…I want it perpendicular to WorldSpace.

wait what are you asking us? all I understand is how do you look at sun by looking at sun, ... sorry I don't understand the Question maybe you have answered your own question. PS. Y is Vector3.up oh sorry, ... Vector3(X,Y,Z); Vector3.up = Vector3(0,1,0);

ahhh now we are getting at local space Y will be up left right and slightly right up or whereever it's rotated at. no you cannot change that because of that feature we have rotating objects otehrwise we'd have all constant Vector3(0,0,0) rotation ALSO if local space is orientated 0,0,0 and parent is looking down note that child local will be same up as parent up so propably looking forward (X/Z)

This isn't really a Unity problem as much as a Vector math problem. Remember that all vectors have direction and magnitude, so you can rotate the direction while keeping the magnitude of the vector which essentially rotates the vector as if it was an object.

You could always just put a child Transform object above your parent object and use the Transform.LookAt(newObject); Any reason specifically you want to rotate the vector? Vectors are just a point in 3d space the Transform is what's rotation would actually be changing if I'm not mistaken. Judging by the other comments it seems I may be wrong on that. But it still seems like a strange way to go about this to me. I guess the vector position is oriented to the transform so you can change it with just vector calculations or something?

Vectors are not points. They are basically a line extending from the origin to the point you set, giving them both direction and magnitude (length of the vector). This is actually the way Unity does it, it just does it for you and all you have to touch is a [function][1]. What he's doing would orbit the vector around its origin (I'm guessing the world origin). [1]: http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html

1 Answer

1

If I understand what you want properly, I think you just need to calculate the magnitude of your Vector and set the Vector to (0,magnitude,0). That would essentially rotate the Vector to point straight up with the same magnitude.

So

Vector3 originalVector = new Vector3(2,8,4);

Vector3 newVector = new Vector3(0,originalVector.magnitude,0);

This method works simply for rotating a vector to an axis, but if you want actual free rotation (such as by degrees/radians) you’ll have to use some trig. I can show you this too if you’d like as it’s really not that hard, just a matter of knowing the equation to use.

If you were working with a transform this same method would by done through RotateAround.

Not exactly but your comment gives me the answer. I wanted the local Y perpendicular ( supposing it was a Transform). So X and Z stay in X,Z WorldSpace Plane. I just set the vector3.y to 0. I will get a shorter vector but it's OK for my purpose,because I normalize it later. myVector = Vector3(myVector.x, 0, myVector.z).normalized; I have seen complicated functions with Sin and Cos for rotating Vectors in this forum post: [http://forum.unity3d.com/threads/33215-Vector-rotation][1]