示例#1
0
        public static bool GetStateClickPoint(State state, out Quaternion quaternion)
        {
            quaternion = default;
            if (Event.current.type != EventType.MouseDown || Event.current.button != 0)
            {
                return(false);
            }
            GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
            Event.current.Use();

            SphereCollider collider = state.GetComponent <SphereCollider>();
            Ray            ray      = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

            ray.origin -= ray.direction * collider.radius * 4;

            if (collider.Raycast(ray, out RaycastHit hit, 100.0f))
            {
                var toCenterDirection = state.transform.position - hit.point;
                var rightDirection    = Vector3.Cross(toCenterDirection, ray.direction);
                var normal            = Vector3.Cross(rightDirection, ray.direction);
                var hitPosition       = state.transform.position + StateEditorWindow.ReflectDirection(toCenterDirection, normal);

                Undo.RecordObject(state, "Undo orientation change");

                quaternion = Quaternion.FromToRotation(Vector3.forward,
                                                       hitPosition - state.transform.position);
                return(true);
            }
            return(false);
        }
示例#2
0
    private static void SceneGui(SceneView sceneView)
    {
        if (!EditableItem || !EditableState)
        {
            return;
        }

        Handles.color = CubeColor;
        Handles.DrawWireCube(
            EditableState.transform.position + EditableItem.orientation * Vector3.forward,
            Vector3.one * 0.2f
            );

        if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
        {
            GUIUtility.hotControl = GUIUtility.GetControlID(FocusType.Passive);
            Event.current.Use();

            SphereCollider collider = EditableState.GetComponent <SphereCollider>();
            Ray            ray      = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

            //if ((state.transform.position - ray.origin).magnitude <= collider.radius * collider.radius)
            ray.origin -= ray.direction * collider.radius * 4;

            if (collider.Raycast(ray, out RaycastHit hit, 100.0f))
            {
                var toCenterDirection = EditableState.transform.position - hit.point;
                var rightDirection    = Vector3.Cross(toCenterDirection, ray.direction);
                var normal            = Vector3.Cross(rightDirection, ray.direction);
                var hitPosition       = EditableState.transform.position + StateEditorWindow.ReflectDirection(toCenterDirection, normal);

                Undo.RecordObject(EditableItem, "Undo orientation change");

                EditableItem.orientation = Quaternion.FromToRotation(Vector3.forward,
                                                                     hitPosition - EditableState.transform.position);
            }
        }
    }