Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Rotating a Vector by an eular angle?

Discussion in 'Scripting' started by DavidLancaster, Feb 26, 2009.

Thread Status:
Not open for further replies.
  1. DavidLancaster

    DavidLancaster

    Joined:
    Feb 26, 2009
    Posts:
    38
    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?
    Code (csharp):
    1.  
    2. // tempVector is a vector pointing from the player to the mouse cursor
    3. 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.
    4. Quaternion tempQ = Quaternion.LookRotation(tempVector); // I now want to find the rotation of this vector
    5.  
    6. tempQ = Quaternion.Euler(0, 30 , 0) * tempQ; // I now what to rotate the rotation by 30 degrees along the y axis
    7. float angleRadians = tempQ2.eulerAngles.y * Mathf.Deg2Rad; // I now find the radians of this angle
    8. 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
    9.  
    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:

    Code (csharp):
    1.  
    2. // Convert the angle into a rotation, by which we then reposition the camera
    3. currentRotation = Quaternion.Euler (0, currentAngle, 0);
    4.  
    5. // Set the position of the camera on the x-z plane to:
    6. // distance meters behind the target
    7. transform.position = targetCenter;
    8. transform.position += currentRotation * Vector3.back * distance;
    9.  
    Would anyone be willing to help?

    Thank you guys! I appreciate it!

    David
     
    akash_virodhia likes this.
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    transform.eulerAngles.x += 30;
    transform.eulerAngles.z -= 120;
    transform.eulerAngles.y = 50;

    As easy as that! Heh.
     
    Siliko likes this.
  3. DavidLancaster

    DavidLancaster

    Joined:
    Feb 26, 2009
    Posts:
    38
    Howdy. Thanks for your response. I'm not looking at rotating a transform object but a Vector3. Any ideas?
     
    Siliko likes this.
  4. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    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.
     
  5. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    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:

    Code (csharp):
    1. Vector3 rotatedVector = Quaternion.AngleAxis(30, Vector3.up) * originalVector;
    If you need to combine several rotations, you can just multiply their quaternions together:

    Code (csharp):
    1. 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.
     
  6. dawvee

    dawvee

    Joined:
    Nov 12, 2008
    Posts:
    276
    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.
     
  7. DavidLancaster

    DavidLancaster

    Joined:
    Feb 26, 2009
    Posts:
    38
    Thanks Gargerath! I'll remember that :)

    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!
     
    umbrella-crash and bhickey94 like this.
  8. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,502
    Error
    "Cannot modify the return value of 'Transform.eulerAngles' because it is not a variable"
     
  9. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,261
    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:
    Code (CSharp):
    1. transform.eulerAngles.y = 50;
    into this:
    Code (CSharp):
    1. Vector3 tmp = transform.eulerAngles;
    2. tmp.y = 50;
    3. transform.eulerAngles = tmp;
     
  10. SmithySFC

    SmithySFC

    Joined:
    Jan 15, 2021
    Posts:
    11
    Thanks so much for this!!!
     
  11. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,661
    There's a "Like" button on the forum so you don't have to dredge up another long-dead thread just to say thanks.
     
  12. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,318
    Yes, but this here, this is meta. He actually quoted a post that starts with "This post was from 2009"
     
Thread Status:
Not open for further replies.