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

Rotating a Point around an Axis

Discussion in 'Scripting' started by sacunha, Jan 21, 2019.

  1. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    I'm trying to rotate a point (Vector3) around a 3D axis/direction.
    So far, I've only found 1 way to do it, wich requires making the object that I want to rotate a child of my object that is going to do the rotation, and face this new object in the direction I want.
    This does not feel correct, because it makes me instatiate an (empty) gameobject for the rotation purpose and I have to mess arround the object's parent that I want to rotate...

    I decided to search for an alternative and found this page (scroll to almost bottom of the page): https://stackoverflow.com/questions/6721544/circular-rotation-around-an-arbitrary-axis
    This seems to be what I need however, its in a math formula and I can't "translate" it to code.

    Image: https://www.dropbox.com/s/5nvfvki31soo0zw/21-01-2019 10-54-06.png?dl=0
     
    Last edited: Jan 21, 2019
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  3. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
  4. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Worked like a charm. Thanks.
     
    eses likes this.
  5. CosmicGiant

    CosmicGiant

    Joined:
    Jul 22, 2014
    Posts:
    23
    sacunha kinda asked for one thing when he needed something quite different, but for anyone having the issue of rotating a point instead of an object, I don't mind sharing these extension methods I have in my personal utils library:

    Code (CSharp):
    1. public static Vector3 Rotated(this Vector3 vector, Quaternion rotation, Vector3 pivot = default(Vector3)) {
    2.     return rotation * (vector - pivot) + pivot;
    3. }
    4.  
    5. public static Vector3 Rotated(this Vector3 vector, Vector3 rotation, Vector3 pivot = default(Vector3)) {
    6.     return Rotated(vector, Quaternion.Euler(rotation), pivot);
    7. }
    8.  
    9. public static Vector3 Rotated(this Vector3 vector, float x, float y, float z, Vector3 pivot = default(Vector3)) {
    10.     return Rotated(vector, Quaternion.Euler(x, y, z), pivot);
    11. }
     
  6. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148

    Will add them to my library as well.
    Just one question. What is the "This" argument for? I'm not getting it.
     
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    CosmicGiant and sacunha like this.
  8. Nilson_innod

    Nilson_innod

    Joined:
    Oct 25, 2022
    Posts:
    2
    where is it library? i need use this methot
     
  9. RafaelGomes00

    RafaelGomes00

    Joined:
    Aug 13, 2020
    Posts:
    73
    This is exactly what I was looking for thank you!!!!