I’ve got a gameobject which I’d like to rotate round a point (which is stored as a serialized field in a component script). In the editor scene tab, I’d like to visualise the rotation point with a handle (like the blue dot handles on a gameobject when using the resize tool), and to be able to set it using the mouse. Is this possible, and where do I look to get started?
Ok, it took a few hours of hunting round to find this simple solution on Reddit. It boiled down to just creating an OnSceneGUI method in a script, which in turn must be placed in the Assets/Editor Directory. Here’s my version, which may be of some use to others.
(RotateController is the class that has RotationCentrePosition as a public Vector2 field, and is added as a component to a GameObject).
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(RotateController))]
public class MoveHandleEditor : Editor
{
void OnSceneGUI()
{
// get the chosen game object
RotateController t = target as RotateController;
if (t == null)
return;
// Get the centre of rotation position.
// working in 2D, so rotationCentrePosition is a Vector2.
Vector3 pos = t.rotationCentrePosition;
pos.z = t.transform.position.z;
// Set the rotation centre point to where ever the handle was moved to.
t.rotationCentrePosition = Handles.PositionHandle(pos, Quaternion.identity);
}
}
1 Like