public static void CreateAreaTriggerRelativeToEntity(Vector3D position, string entityName, float radius, string name)
        {
            var trigger = new MyAreaTriggerComponent(name);
            trigger.Radius = radius;

            MyEntity entity;
            if (MyEntities.TryGetEntityByName(entityName, out entity))
            {
                trigger.Center = position;
                trigger.DefaultTranslation = position - entity.PositionComp.GetPosition();
                if (!entity.Components.Contains(typeof(MyTriggerAggregate)))
                    entity.Components.Add(typeof(MyTriggerAggregate), new MyTriggerAggregate());
                entity.Components.Get<MyTriggerAggregate>().AddComponent(trigger);
            }
        }
        public static long CreateAreaTriggerOnPosition(Vector3D position, float radius, string name)
        {
            var trigger = new MyAreaTriggerComponent(name);
            var entity = new MyEntity();
            trigger.Radius = radius;
            entity.PositionComp.SetPosition(position);
            entity.EntityId = MyEntityIdentifier.AllocateId();
            trigger.DefaultTranslation = Vector3D.Zero;

            MyEntities.Add(entity);
            if (!entity.Components.Contains(typeof(MyTriggerAggregate)))
                entity.Components.Add(typeof(MyTriggerAggregate), new MyTriggerAggregate());
            entity.Components.Get<MyTriggerAggregate>().AddComponent(trigger);

            return entity.EntityId;
        }
        private bool TryPutTriggerOnEntity()
        {
            var worldMatrix = MyAPIGateway.Session.Camera.WorldMatrix;
            List<MyPhysics.HitInfo> hits = new List<MyPhysics.HitInfo>();
            MyPhysics.CastRay(worldMatrix.Translation, worldMatrix.Translation + worldMatrix.Forward * 30, hits, 15);

            foreach (var hitInfo in hits)
            {
                var body = (MyPhysicsBody)hitInfo.HkHitInfo.Body.UserObject;
                if (body.Entity is MyCubeGrid)
                {
                    var rayEntity = (MyEntity)body.Entity;
                    var dialog = new ValueGetScreenWithCaption("Entity Spawn on: " + rayEntity.DisplayName, "", delegate(string text)
                    {
                        if (m_selectedTrigger != null)
                            m_selectedTrigger.CustomDebugColor = Color.Red;

                        m_selectedTrigger = new MyAreaTriggerComponent(text);
                        if (!rayEntity.Components.Contains(typeof(MyTriggerAggregate)))
                            rayEntity.Components.Add(typeof(MyTriggerAggregate), new MyTriggerAggregate());
                        rayEntity.Components.Get<MyTriggerAggregate>().AddComponent(m_selectedTrigger);
                        m_selectedTrigger.Center = MyAPIGateway.Session.Camera.Position;
                        m_selectedTrigger.Radius = 2;

                        m_selectedTrigger.CustomDebugColor = Color.Yellow;

                        return true;
                    });
                    MyGuiSandbox.AddScreen(dialog);
                    return true;
                }
            }
            return false;
        }
        public bool SpawnTrigger()
        {
            var dialog = new ValueGetScreenWithCaption("Spawn new Trigger", "", delegate(string text)
            {
                var trigger = new MyAreaTriggerComponent(text);
                var entity = new MyEntity();
                trigger.Radius = 2;
                trigger.Center = MyAPIGateway.Session.Camera.Position;
                entity.PositionComp.SetPosition(MyAPIGateway.Session.Camera.Position);
                entity.PositionComp.LocalVolume = new BoundingSphere(Vector3.Zero, 0.5f);
                entity.EntityId = MyEntityIdentifier.AllocateId();
                entity.Components.Remove<MyPhysicsComponentBase>();
                entity.Components.Remove<MyRenderComponentBase>();
                entity.DisplayName = "TriggerHolder";
                MyEntities.Add(entity);

                if (!entity.Components.Contains(typeof(MyTriggerAggregate)))
                    entity.Components.Add(typeof(MyTriggerAggregate), new MyTriggerAggregate());
                entity.Components.Get<MyTriggerAggregate>().AddComponent(trigger);


                if(m_selectedTrigger != null)
                    m_selectedTrigger.CustomDebugColor = Color.Red;;

                m_selectedTrigger = trigger;
                m_selectedTrigger.CustomDebugColor = Color.Yellow;
                return true;
            });
            MyGuiSandbox.AddScreen(dialog);

            return true;
        }
        public bool DeleteTrigger()
        {
            if(m_selectedTrigger == null)
                return false;

            if(m_selectedTrigger.Entity.DisplayName == "TriggerHolder")
                m_selectedTrigger.Entity.Close();
            else
            {
                m_selectedTrigger.Entity.Components.Remove(typeof(MyAreaTriggerComponent), m_selectedTrigger);
            }

            m_selectedTrigger = null;

            return true;
        }
        private bool SelectTrigger()
        {
            var position = MyAPIGateway.Session.Camera.Position;
            var minLength = double.MaxValue;

            if(m_selectedTrigger != null)
                m_selectedTrigger.CustomDebugColor = Color.Red;

            foreach (var trigger in m_queriedTriggers)
            {
                var length = (trigger.Center - position).LengthSquared();
                if(length < minLength)
                {
                    minLength = length;
                    m_selectedTrigger = trigger;
                }
            }

            if(Math.Abs(minLength - double.MaxValue) < double.Epsilon)
                m_selectedTrigger = null;

            if(m_selectedTrigger != null)
                m_selectedTrigger.CustomDebugColor = Color.Yellow;

            return true;
        }
        private void AttachTriggerOnClick(MyGuiControlButton myGuiControlButton)
        {
            if(m_transformSys.ControlledEntity == null) return;

            var selectedEntity = m_transformSys.ControlledEntity;
            var dialog = new ValueGetScreenWithCaption("Entity Spawn on: " + m_transformSys.ControlledEntity.DisplayName,
                "", 
                delegate(string text)
            {
                var areaTrigger = new MyAreaTriggerComponent(text);
                m_triggerManipulator.SelectedTrigger = areaTrigger;

                if (!selectedEntity.Components.Contains(typeof(MyTriggerAggregate)))
                {
                    // Add agregate if its missing
                    selectedEntity.Components.Add(typeof(MyTriggerAggregate), new MyTriggerAggregate());
                }
                // add trigger it self
                selectedEntity.Components.Get<MyTriggerAggregate>().AddComponent(m_triggerManipulator.SelectedTrigger);
                // Init trigger dimensions
                areaTrigger.Center = MyAPIGateway.Session.Camera.Position;
                areaTrigger.Radius = 2;
                // Selected color
                areaTrigger.CustomDebugColor = Color.Yellow;

                return true;
            });
            MyGuiSandbox.AddScreen(dialog);
        }