Rotating a Point around an Axis

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): 3d - Circular rotation around an arbitrary axis - Stack Overflow
This seems to be what I need however, its in a math formula and I can’t “translate” it to code.

Image: Dropbox - File Deleted - Simplify your life

Hi @sacunha

“object that I want to rotate”

So if you do have an object you want to rotate and not just a position that you want to manipulate, this might be helpful:

Its a start! will take a look at it, Thanks

Worked like a charm. Thanks.

1 Like

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:

public static Vector3 Rotated(this Vector3 vector, Quaternion rotation, Vector3 pivot = default(Vector3)) {
    return rotation * (vector - pivot) + pivot;
}

public static Vector3 Rotated(this Vector3 vector, Vector3 rotation, Vector3 pivot = default(Vector3)) {
    return Rotated(vector, Quaternion.Euler(rotation), pivot);
}

public static Vector3 Rotated(this Vector3 vector, float x, float y, float z, Vector3 pivot = default(Vector3)) {
    return Rotated(vector, Quaternion.Euler(x, y, z), pivot);
}
11 Likes

Will add them to my library as well.
Just one question. What is the “This” argument for? I’m not getting it.

@sacunha

Well @XenoRo wrote those methods, but I can answer…

“this” is needed when defining extension methods.

You have to use that modifier to specify what type the method is extending.

See: Extension Methods - C# | Microsoft Learn

2 Likes

where is it library? i need use this methot

This is exactly what I was looking for thank you!!!