Search Unity

how to find a new vector direction from exsisting one

Discussion in 'Scripting' started by Most_Incredible, Sep 9, 2014.

  1. Most_Incredible

    Most_Incredible

    Joined:
    Jan 25, 2012
    Posts:
    36
    Hi ,

    transform.translate(vector3.up * speed * time.deltatime);
    but I want to move my gameObject with varied angle from vector3.up.

    transform.translate(newDirectionvector * speed * time.deltatime);
    my approach :
    what I assume is from the vector math
    NewVector = OldVector * Cos(thetha); // from dot product formula
    input will be any value theta 0 to 360.
    will this approach gives me a new direction vector.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Quaternions should be used here. Now I'm not sure which axis you want to rotate around since you're in 3D, but I'll assume theta is one of the euler angles you want to rotate by.
    Code (csharp):
    1. Vector3 rotatedVector = Quaternion.Euler(0f, 0f, theta) * originalVector;
    Once you have a Quaternion representing a rotation, you multiply Quaternion * Vector to rotate the Vector by that Quaternion. You cannot put the Quaternion on the right, Eg: Vector * Quaternion (ERROR) as the matrix math is undefined!
     
    Most_Incredible likes this.
  3. Most_Incredible

    Most_Incredible

    Joined:
    Jan 25, 2012
    Posts:
    36
    Thanks Gath Smith,

    It Worked as I expected.
    Thanks for u r quick reply too.