Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Quaternion rotate point around pivot?

Discussion in 'Scripting' started by davidk_43, Feb 19, 2015.

  1. davidk_43

    davidk_43

    Joined:
    Feb 19, 2015
    Posts:
    10
    Hi all,

    Newbie here. I've seen similar questions asked but without a clear enough answer for me. I'm trying to rotate a Vector3 point by x degrees based on another Vector3 point acting as the pivot. For now I'm just looking for a rotation around Y axis and distance between the point and pivot never changes.

    Closest I've got is something like:
    Vector3 point = new Vector3 (0, 0, 0);
    Vector3 pivot = new Vector3(2,0,2);
    float angle = 30;
    Quaternion rotate = Quaternion.AngleAxis(angle, Vector3.up) ;
    Vector3 new_point = rotate * point;

    However Quaternion.AngleAxis seems to only rotate around the World Y Axis, how can I do the equivalent with the pivot vector acting as the placement for the Y axis? I can achieve this in the editor with GameObjects but want to be able to translate straight vector points in space if possible.

    This may just need to be some manual C# code to calculate this but I suspect Unity have supplied a way to do this with Quaternions and I just can't work it out.

    Kind regards and thanks for any help!
    David
     
    Last edited: Feb 19, 2015
  2. Buddy_Redmond

    Buddy_Redmond

    Joined:
    Aug 30, 2014
    Posts:
    23
    You can use transform.RotateAround(). I set up a scene with a cube as my target. Here is some code that makes the camera (or other attached object) rotate around the cube:

    Code (CSharp):
    1. public Transform target;
    2.     public float speed = 10;
    3.  
    4.     void Update() {
    5.         transform.RotateAround(target.position, target.up, speed * Time.deltaTime);
    6.     }
    This rotates around the cube's up vector.
     
    beado likes this.
  3. davidk_43

    davidk_43

    Joined:
    Feb 19, 2015
    Posts:
    10
    Hi, thanks for the quick reply!

    I'm looking to rotate Vector3 points which as far as I'm aware don't have any transform properties.

    I might be looking at the problem wrong but currently am looking to manipulate a list of vectors that make up a path that gets used later, not at the time of the rotate.

    Cheers
    David
     
  4. Buddy_Redmond

    Buddy_Redmond

    Joined:
    Aug 30, 2014
    Posts:
    23
    YaserDev likes this.
  5. davidk_43

    davidk_43

    Joined:
    Feb 19, 2015
    Posts:
    10
    Thanks. I've actually tried that code - I've got it working now, but it wasn't earlier - think I had misaligned loops from capturing the rotation to applying to my list. Still good to see even though a tiny bit of math in there the Quaternion.Euler is still useful. Multiplying a quaternion with a vector is interesting to say the least!
     
  6. wechat_os_Qy06eaOhICF9NcZoMWMLtv5cI

    wechat_os_Qy06eaOhICF9NcZoMWMLtv5cI

    Joined:
    Feb 1, 2018
    Posts:
    33
    What if you want rotate by X and Y ....Invoke this this function two times?
     
    owlrazum likes this.