Rotating a Vector by an eular angle?

Hey Guys!

I have searched the forums, sorry if this question is already answered else where.

Quaternions are great, I think I have a basic grasp on how to work with those, however let’s say I have a Vector3, and I want to rotate that Vector 30 degrees? So that I can create a bullet at that position.

This is how I am doing it so far, I am wanting to know, is there an easier way to rotate this Vector?

// tempVector is a vector pointing from the player to the mouse cursor
Vector3 createPosition = transform.position + (tempVector.normalized * 0.7f); // transform is the player's position, I want to create the bullet towards the mouse cursor 0.7 units away from the player.
Quaternion tempQ = Quaternion.LookRotation(tempVector); // I now want to find the rotation of this vector

tempQ = Quaternion.Euler(0, 30 , 0) * tempQ; // I now what to rotate the rotation by 30 degrees along the y axis
float angleRadians = tempQ2.eulerAngles.y * Mathf.Deg2Rad; // I now find the radians of this angle
createPosition = new Vector3(transform.position.x + (Mathf.Sin(angleRadians) * 0.7f),1,transform.position.z + (Mathf.Cos(angleRadians) * 0.7f));  //using trigonometry I now calculate the new position which is 30 degrees to the right of my original position

I am using C# and for some odd reason I have had a little difficulty understanding ‘SmoothFollowCamera.js’, as I haven’t found a way to put this code into C# when I do I get complaints about currentRotation being a Quaternion and multiplying it with another type such as Vector3 and a float…

At line 141 of SmoothFollowCamera.js:

// Convert the angle into a rotation, by which we then reposition the camera
currentRotation = Quaternion.Euler (0, currentAngle, 0);

// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = targetCenter;
transform.position += currentRotation * Vector3.back * distance;

Would anyone be willing to help?

Thank you guys! I appreciate it!

David

1 Like

transform.eulerAngles.x += 30;
transform.eulerAngles.z -= 120;
transform.eulerAngles.y = 50;

As easy as that! Heh.

1 Like

Howdy. Thanks for your response. I’m not looking at rotating a transform object but a Vector3. Any ideas?

1 Like

Well, just have the transform of an empty object represent that Vector3 and you’re all set! Have the empty as a child of another empty that’s located at 0,0,0. Whenever you rotate the zeroed transform you’ll rotate the transform with the Vector3 and get the new value.

I don’t know any functions that would rotate the Vector3 directly.

If you check the Quaternion docs, you’ll see that there are a set of functions that create new rotations based upon various kinds of input. You’ve already used Quaternion.Euler, which is fine, but if you just want a rotation around a single axis, or if you want to combine individual rotations in some specific order, Quaternion.AngleAxis is generally preferable.

In order to rotate a vector by a quaternion, you multiply the two together:

Vector3 rotatedVector = Quaternion.AngleAxis(30, Vector3.up) * originalVector;

If you need to combine several rotations, you can just multiply their quaternions together:

Quaternion rotation = Quaternion.Euler(x, y, z) * Quaternion.AngleAxis(angle, Vector3.up);

The order in which you multiply quaternions is important, so if you don’t get the result you expected from A * B, try it again with B * A.

You said you had difficulty using quaternion * vector in C#, but it is definitely possible. If you still have problems, post the code and the exact error message and I’ll try to diagnose the problem.

25 Likes

Adding on to NCarter’s excellent reply, one thing to note when multiplying Quaternions, Vectors and any other Matrix-y variables is that the order of the variables matters. A * B does not equal B * A, and one might not even be possible.

If you look at this in the Quaternion docs you’ll see that multiplication by a Vector3 is defined, so it should work, but it’s only defined for Quaternion * Vector3, not Vector3 * Quaternion.

That said, I have no idea if this is the problem you were having, just a WAG.

5 Likes

Thanks Gargerath! I’ll remember that :slight_smile:

NCarter I really appreciate the indepth explanation, it makes perfect sense and works! Thank you!

dawvee I think that was why I was having difficulty with multiplying Vectors and Quaternions, thank you!

2 Likes

Error
“Cannot modify the return value of ‘Transform.eulerAngles’ because it is not a variable”

This post was from 2009. The code in question was UnityScript and not C#. UnityScript did have support for this and turned a line like this:

transform.eulerAngles.y = 50;

into this:

Vector3 tmp = transform.eulerAngles;
tmp.y = 50;
transform.eulerAngles = tmp;
3 Likes

Thanks so much for this!!!

There’s a “Like” button on the forum so you don’t have to dredge up another long-dead thread just to say thanks.

3 Likes

Yes, but this here, this is meta. He actually quoted a post that starts with “This post was from 2009”