예제 #1
0
        public virtual ProposedPosition GetPosition(Vector3 mousePosition, bool snapToGrid, float gridSize)
        {
            var cameraPosition     = cam.transform.position;
            var mouseWorldPosition = cam.ScreenToWorldPoint(new Vector3(mousePosition.x, mousePosition.y, cam.nearClipPlane));
            var viewVector         = mouseWorldPosition - cameraPosition;

            cameraPosition.y += rayYOffset;

            var   gridPositon = Camera.Grid.instance.transform.position;
            float gridY       = gridPositon.y;

            double  t        = (gridY - cameraPosition.y) / viewVector.y;
            Vector3 hitPoint = gridPositon;
            double  x        = (double)cameraPosition.x + (double)viewVector.x * t;
            double  y        = (double)cameraPosition.z + (double)viewVector.z * t;

            hitPoint.x = (float)x;
            hitPoint.z = (float)y;

            var position = new ProposedPosition(brick.transform);

            position.position   = hitPoint + positionOffset;
            position.position.y = -lowestYoffset;
            position.position   = CorrectPosition(position.position, snapToGrid, gridSize);
            position.isValid    = true;

            return(position);
        }
예제 #2
0
        public override ProposedPosition GetPosition(Vector3 mousePosition, bool snapToGrid, float gridSize)
        {
            var ray = cam.ScreenPointToRay(mousePosition);

            ray.origin -= dragPointOffset;
            var mouseRayDirection = Quaternion.LookRotation(ray.direction);

            //calculate bounds dimensions
            Bounds bounds            = brick.GetBounds();
            float  minBrickDimension = Mathf.Min(bounds.extents.x, bounds.extents.y, bounds.extents.z);

            bounds.extents = new Vector3(minBrickDimension, minBrickDimension, 1000f);
            bounds.center  = brick.transform.position + dragPointOffset;

            //move bouds to position in fornt of the camera
            var distanceBricCamera = bounds.extents.z - (cam.transform.position - bounds.center).magnitude;

            bounds.center += ray.direction * distanceBricCamera;

            //check box collision with other bricks
            var colliders = Physics.OverlapBox(bounds.center, bounds.extents, mouseRayDirection, 1);
//            DrawBounds(bounds, mouseRayDirection, Color.red);

            //select nearest point
            Vector3 closestPoint     = Vector3.zero;
            float   shortestDistance = 10000f;
            bool    found            = false;

            foreach (var collider in colliders)
            {
                if (collider.tag != "background" && !ContainCollider(brick, collider))
                {
                    var point    = collider.bounds.ClosestPoint(ray.origin);
                    var distance = (ray.origin - point).magnitude;
                    if (distance < shortestDistance)
                    {
                        closestPoint     = point;
                        shortestDistance = distance;
                        found            = true;
                    }
                }
            }

            if (found)
            {
                //calculate new brick position
                float   distance = shortestDistance;
                Vector3 point    = ray.origin + ray.direction * distance;

                ProposedPosition position = new ProposedPosition(brick.transform);
                position.position = CorrectPosition(point, snapToGrid, gridSize);
                position.isValid  = false;

                if (position.position.y + lowestYoffset >= 0)
                {
                    return(position);
                }
            }

            //fall back positioning to grid positioner
            return(base.GetPosition(mousePosition, snapToGrid, gridSize));
        }