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
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: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html
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): 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); }
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: https://docs.microsoft.com/en-us/do...g-guide/classes-and-structs/extension-methods