Exemple #1
0
        /// <summary>
        /// Check weather the "type" in the screen space. (Character Controller)
        /// </summary>
        /// <param name="cap"> character controller to check. (Collider) </param>
        /// <returns>
        /// true: in screen space,
        /// false: not in screen space
        /// </returns>
        public bool CheckInScreenSpace(CharacterController cap)
        {
            // First Step: Find CharacterController's 4 bounds.
            Vector3 capScale = cap.transform.localScale;

            capScale = JCS_Mathf.AbsoluteValue(capScale);

            Vector3 cCenter = new Vector3(
                cap.center.x * capScale.x,
                cap.center.y * capScale.y,
                cap.center.z * capScale.z);

            float cR = cap.radius * capScale.x;
            float cH = (cap.height - (cap.radius * 2.0f)) * capScale.y;

            if (cH < 0)
            {
                cH = 0;
            }

            float cTopBound   = cap.transform.position.y + cCenter.y + (cH / 2.0f) + cR;
            float cBotBound   = cap.transform.position.y + cCenter.y - (cH / 2.0f) - cR;
            float cRightBound = cap.transform.position.x + cCenter.x + cR;
            float cLeftBound  = cap.transform.position.x + cCenter.x - cR;

#if (UNITY_EDITOR)
            Vector3 cOrigin = cap.transform.position + cCenter;
            Debug.DrawLine(cOrigin,
                           new Vector3(cOrigin.x, cTopBound, cOrigin.z));
            Debug.DrawLine(cOrigin,
                           new Vector3(cOrigin.x, cBotBound, cOrigin.z));
            Debug.DrawLine(cOrigin,
                           new Vector3(cRightBound, cOrigin.y, cOrigin.z));
            Debug.DrawLine(cOrigin,
                           new Vector3(cLeftBound, cOrigin.y, cOrigin.z));
#endif

            // Next step: find camera 4 bounds.
            Camera cam = JCS_Camera.main.GetCamera();

            JCS_Canvas jcsCanvas = JCS_Canvas.instance;

            Vector3 camPos = cam.transform.position;
            // only need to know the depth.
            {
                camPos.x = 0;
                camPos.y = 0;
            }
            Vector3 canvasPos = jcsCanvas.transform.position;
            // only need to know the depth.
            {
                canvasPos.x = 0;
                canvasPos.y = 0;
            }
            float camToCanvasDistance = Vector3.Distance(camPos, canvasPos);

            Vector3 gameDepth = new Vector3(0, cap.transform.position.z, 0);
            float   camToGameDepthDistance = Vector3.Distance(camPos, gameDepth);

            //print("To Game depth Distance: " + camToGameDepthDistance);
            //print("To Cavas Distnace: " + camToCanvasDistance);

            Vector2 canvasRect = jcsCanvas.GetAppRect().sizeDelta;
            // transfer rect from screen space to world space
            {
                canvasRect.x *= jcsCanvas.GetAppRect().localScale.x;
                canvasRect.y *= jcsCanvas.GetAppRect().localScale.y;
            }

            Vector3 gameRect = new Vector3(
                camToGameDepthDistance * canvasRect.x / camToCanvasDistance,
                camToGameDepthDistance * canvasRect.y / camToCanvasDistance,
                0);

            // camPos name are named up there.
            // cannot name the same.
            Vector3 cCamPos = cam.transform.position;

            float camTopBound   = cCamPos.y + gameRect.y / 2;
            float camBotBound   = cCamPos.y - gameRect.y / 2;
            float camRightBound = cCamPos.x + gameRect.x / 2;
            float camLeftBound  = cCamPos.x - gameRect.x / 2;

#if (UNITY_EDITOR)
            Vector3 topLeft = cam.transform.position;
            topLeft.x -= gameRect.x / 2;
            topLeft.y += gameRect.y / 2;

            Vector3 topRight = cam.transform.position;
            topRight.x += gameRect.x / 2;
            topRight.y += gameRect.y / 2;

            Vector3 botLeft = cam.transform.position;
            botLeft.x -= gameRect.x / 2;
            botLeft.y -= gameRect.y / 2;

            Vector3 botRight = cam.transform.position;
            botRight.x += gameRect.x / 2;
            botRight.y -= gameRect.y / 2;

            // set depth to the same
            topLeft.z  = mGameDepth;
            topRight.z = mGameDepth;
            botLeft.z  = mGameDepth;
            botRight.z = mGameDepth;

            // Draw the box
            JCS_Debug.DrawRect(topLeft, topRight, botRight, botLeft);
#endif

            if (cRightBound < camLeftBound ||
                camRightBound < cLeftBound ||
                cTopBound < camBotBound ||
                camTopBound < cBotBound)
            {
                // no in the screen
                return(false);
            }

            // in screen
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// Square inside the game editor screen. Display
        /// the screen width and height in current game depth.
        /// 2D Game probably need this. 3D Game is optional.
        /// </summary>
        private void DisplayGameDepthCamera()
        {
            if (!mDisplayGameDepthCamera)
            {
                return;
            }

            // Next step: find camera 4 bounds.
            Camera cam = JCS_Camera.main.GetCamera();

            JCS_Canvas jcsCanvas = JCS_Canvas.instance;

            Vector3 camPos = cam.transform.position;
            // only need to know the depth.
            {
                camPos.x = 0;
                camPos.y = 0;
            }
            Vector3 canvasPos = JCS_Canvas.instance.transform.position;
            // only need to know the depth.
            {
                canvasPos.x = 0;
                canvasPos.y = 0;
            }
            float camToCanvasDistance = Vector3.Distance(camPos, canvasPos);

            Vector3 gameDepth = new Vector3(0, mGameDepth, 0);
            float   camToGameDepthDistance = Vector3.Distance(camPos, gameDepth);

            //print("To Game depth Distance: " + camToGameDepthDistance);
            //print("To Cavas Distnace: " + camToCanvasDistance);

            Vector2 canvasRect = jcsCanvas.GetAppRect().sizeDelta;

            // transfer rect from screen space to world space
            {
                canvasRect.x *= jcsCanvas.GetAppRect().localScale.x;
                canvasRect.y *= jcsCanvas.GetAppRect().localScale.y;
            }

            mCamRectSize = new Vector3(
                camToGameDepthDistance * canvasRect.x / camToCanvasDistance,
                camToGameDepthDistance * canvasRect.y / camToCanvasDistance,
                0);

            // camPos name are named up there.
            // cannot name the same.
            Vector3 cCamPos = cam.transform.position;

            float camTopBound   = cCamPos.y + mCamRectSize.y / 2;
            float camBotBound   = cCamPos.y - mCamRectSize.y / 2;
            float camRightBound = cCamPos.x + mCamRectSize.x / 2;
            float camLeftBound  = cCamPos.x - mCamRectSize.x / 2;

            // top left -> bot right
            mCamRect.x      = camLeftBound;
            mCamRect.y      = camTopBound;
            mCamRect.width  = camRightBound;
            mCamRect.height = camBotBound;

            Vector3 topLeft = cam.transform.position;

            topLeft.x -= mCamRectSize.x / 2;
            topLeft.y += mCamRectSize.y / 2;

            Vector3 topRight = cam.transform.position;

            topRight.x += mCamRectSize.x / 2;
            topRight.y += mCamRectSize.y / 2;

            Vector3 botLeft = cam.transform.position;

            botLeft.x -= mCamRectSize.x / 2;
            botLeft.y -= mCamRectSize.y / 2;

            Vector3 botRight = cam.transform.position;

            botRight.x += mCamRectSize.x / 2;
            botRight.y -= mCamRectSize.y / 2;

            // set depth to the same
            topLeft.z  = mGameDepth;
            topRight.z = mGameDepth;
            botLeft.z  = mGameDepth;
            botRight.z = mGameDepth;

            // Draw the box
            JCS_Debug.DrawRect(topLeft, topRight, botRight, botLeft, mGameCamColor);
        }