Search Unity

Rotate direction with x degrees

Discussion in 'Scripting' started by opeca, Oct 7, 2014.

  1. opeca

    opeca

    Joined:
    Sep 17, 2013
    Posts:
    19
    Hello All,

    I get a direction from two Vector3, and I would like to get another direction wich is based on this direction but rotated with x degrees.

    Can somebody help me to get the green direction vector? (example in attachment)


    Thank you! example.jpg
     
  2. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    281
    This is pretty straightforward. You can multiply a vector with a quaternion in order to rotate it. Always multiply a quaternion with a vector; it doesn't work the other way around.

    Code (csharp):
    1.  
    2. Quaternion rotation = Quaternion.AngleAxis(degrees,Vector3.up);
    3. Vector3 green = rotation*red;
    4.  
    I assume you want to rotate it around the y-axis. If you want to rotate it around another axis, use Vector3.forward and Vector3.left for the x and z axes. I don't remember which is which.

    Also, if you only want to get a vector that's rotated 90 degrees, you can use Vector3.Cross.
     
    Last edited: Oct 7, 2014
    opeca likes this.
  3. opeca

    opeca

    Joined:
    Sep 17, 2013
    Posts:
    19
    Thank you very much, it's working!