Внеэкранный индикатор в Unity 3d - PullRequest
0 голосов
/ 11 марта 2019

Я делаю 3d-игру, в которой я случайно вызываю врага, и из этого я хочу показать местоположение врага на экране (знак стрелки), как указание, чтобы игрок мог понять, откуда появляется каждый новый враг. Enemy Indicator Image , как показано на этом изображении, то же самое, что я пытаюсь достичь.Застрял здесь на долгое время.

        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);
        }

Пожалуйста, помогите.в чем моя ошибка, которую я здесь не получаю, потому что я не получаю результат согласно изображению, которое я показал.

Заранее спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...