首页 > 解决方案 > Unity 3d 中的离屏指示器

问题描述

我正在制作一个 3d 游戏,我在其中随机生成敌人,然后我想在屏幕上显示敌人的位置(箭头标记),就像一个指示一样,以便玩家能够了解每个新敌人从哪里来。这张图片中显示的敌人指示器图像与我想要实现的完全一样。卡在这里很久了。

        private ECCCar eccCar;
        private GameObject indicator;

        private Vector2 screenLimit;
        private Vector3 copOffset;
        private bool isAboveCop;
        private float cameraPos;
        private float copAxisOffset;
        private float screenAxisLimit;
        private float distToObj;

        private Vector3 toObj;
        private Vector3 axis;
        private float adjacent;
        private float hypotenuous;
        private float theta;
        private float pos;
        private float copPos;

        void Start()
        {
            arrow.gameObject.SetActive(true);
            eccCar = FindObjectOfType<ECCCar>();
            indicator = Instantiate(arrow.gameObject, transform.position, Quaternion.identity);

            indicator.transform.parent = gameObject.transform;
            screenLimit = DeterminScreenLimit();
        }

        void Update()
        {
            isAboveCop = IsAbove();
            toObj = transform.position - eccCar.transform.position;

            SetAxisHorizontal();
            PerformTrig();

            indicator.transform.position = (Vector3)(eccCar.transform.position) + (toObj.normalized * hypotenuous);
        }

        private void FixedUpdate()
        {
            copOffset = DetermineCopOffset();
        }

        private Vector2 DeterminScreenLimit()
        {
            float x = (Camera.main.orthographicSize * Screen.width / Screen.height) - (arrow.GetComponent<RectTransform>().rect.x / 2);
            float y = (Camera.main.orthographicSize) - (arrow.GetComponent<RectTransform>().rect.x / 2);

            return new Vector2(x, y);
        }

        private bool IsAbove()
        {
            return transform.position.y > eccCar.transform.position.z ? true : false;
        }

        private Vector2 DetermineCopOffset()
        {
            float x = Camera.main.transform.position.x - eccCar.transform.position.x;
            float z = Camera.main.transform.position.y - eccCar.transform.position.z;

            return new Vector2(x, z);
        }

        private float FindAngleToCorner()
        {
            return Vector2.Angle(CreateVectorToAngle(), Vector2.right);
        }

        private Vector2 CreateVectorToAngle()
        {
            return FindWorldCorner(isAboveCop ? 1 : -1) - (Vector2)eccCar.transform.position;
        }

        private Vector2 FindWorldCorner(int modY)
        {
            float x = Camera.main.transform.position.x + screenLimit.x;
            float y = Camera.main.transform.position.y + (screenLimit.y * modY);

            return new Vector2(x, y);
        }

        private void SetAxisHorizontal()
        {
            axis = Vector2.right;
            pos = transform.position.x;
            copPos = eccCar.transform.position.x;
            cameraPos = Camera.main.transform.position.x;
            copAxisOffset = copOffset.x;
            screenAxisLimit = screenLimit.x;
        }

        private void SetAxisVertical()
        {
            axis = Vector2.up;
            pos = transform.position.y;
            copPos = eccCar.transform.position.z;
            cameraPos = Camera.main.transform.position.y;
            copAxisOffset = copOffset.z;
            screenAxisLimit = screenLimit.y;
        }

        private void PerformTrig()
        {
            FindTheta();
            SetAxis();
            FindAdjacent();
            FindDistToObj();
            ClampAdjacent();
            FindHypotanuous();
        }

        private void FindTheta()
        {
            theta = Vector2.Angle(toObj, axis);

            if(theta > 90)
            {
                theta = 180 - theta;
            }
        }

        private void SetAxis()
        {
            if(theta < FindAngleToCorner())
            {
                SetAxisHorizontal();
            }
            else
            {
                SetAxisVertical();
                FindTheta();
            }
        }

        private void FindAdjacent()
        {
            adjacent = Mathf.Abs(pos - copPos);
        }

        private void FindDistToObj()
        {
            distToObj = pos - cameraPos;
        }

        private void ClampAdjacent()
        {
            if(Mathf.Abs(distToObj) > screenAxisLimit)
            {
                float normalized = distToObj / Mathf.Abs(distToObj);
                adjacent = screenAxisLimit + (copAxisOffset * normalized);

                EnableIndicatorScript(true);
            }
            else
            {
                EnableIndicatorScript(false);
            }
        }

        private void FindHypotanuous()
        {
            theta *= Mathf.Deg2Rad;
            hypotenuous = adjacent / Mathf.Cos(theta);
        }

        private void EnableIndicatorScript(bool show)
        {
            indicator.GetComponent<RectTransform>().gameObject.SetActive(show);
        }

        public void DestroyIndicator()
        {
            Destroy(indicator);
        }

请帮忙。我在这里没有得到什么错误,因为我没有根据我显示的图像得到结果。

先感谢您

标签: unity3d

解决方案


推荐阅读