当相机移动时,如何使内部操纵杆保持在屏幕上?

发布于 2025-02-12 11:43:17 字数 3275 浏览 0 评论 0原文

我是第一次开发手机游戏,及其2D自上而下的射手。我有一个问题,当相机移动跟随播放器时,操纵杆的中部被抛在后面。当相机静止时,一切正常,但是我真的不知道如何在相机移动时使中央操纵杆保持在正确的位置。我真的很感谢我如何做到这一点,但我宁愿自己写代码以进行自我完善。

(我知道代码是一团糟,对不起)

   void Update()
    {
        touchNum = Input.touchCount;
        if (touchNum > 0)
        {

            for (int i = 0; i <= (touchNum - 1); i++)
            {
                touch = Input.GetTouch(i);
                touchPosition1 = Camera.main.ScreenToWorldPoint(touch.position);
                if (area.OverlapPoint(touchPosition1))
                {
                    switch (touch.phase)
                    {
                        case TouchPhase.Began:
                            startPos = touchPosition1;
                            joyBack.transform.position = startPos;
                            joyFront.transform.position = startPos;
                            joyBack.SetActive(true);
                            joyFront.SetActive(true);
                            joyFront.transform.position = startPos;
                            joyBack.transform.position = startPos;
                            Debug.Log("begun");
                            break;

                        case TouchPhase.Moved:
                            direction = touchPosition1 - startPos;
                            Vector2 touchPosition2 = Vector2.ClampMagnitude(direction, 1.75f);
                            horizontal = touchPosition2.x;
                            vertical = touchPosition2.y;
                            //Debug.Log(horizontal);
                            distance = horizontal * horizontal + vertical * vertical;
                            distance = Mathf.Sqrt(distance);
                            direction = direction / distance;
                            direction = Limiters(direction, limit);
                            joyFront.transform.position =new Vector2(startPos.x + touchPosition2.x, startPos.y + touchPosition2.y);
                            Debug.DrawLine(startPos, startPos + touchPosition2);
                            Debug.Log(direction);
                            break;

                        case TouchPhase.Ended:
                            distance = 0;
                            Debug.Log("Ended");
                            joyBack.SetActive(false);
                            joyFront.SetActive(false);
                            break;

                    }
                }
                else
                {
                    joyBack.SetActive(false);
                    joyFront.SetActive(false);
                    distance = 0;
                }

            }
        }
    }
    private static Vector2 Limiters(Vector2 direction, float limit)
    {
        if (direction.x > limit)
        {
            direction.x = limit;
        }
        else if (direction.x < -limit)
        {
            direction.x = -limit;
        }
        if (direction.y > limit)
        {
            direction.y = limit;
        }
        else if (direction.y < -limit)
        {
            direction.y = -limit;
        }
        return direction;
    }
    private void FixedUpdate()
    {
        move.Translate((direction) * movespeed * distance * Time.fixedDeltaTime);

    }
}

I'm developing a mobile game for the first time, and its 2D top-down shooter. I'm having an issue where the middle part of the joystick gets left behind when the camera moves to follow the player. Everything works fine when the camera is stationary, but I really have no idea how to make the central joystick stay in the correct position when the camera moves. I would really appreciate a suggestion on how I could do this, but I'd rather write the code myself for self-improvement.

(I know the code is a mess, I'm sorry)

   void Update()
    {
        touchNum = Input.touchCount;
        if (touchNum > 0)
        {

            for (int i = 0; i <= (touchNum - 1); i++)
            {
                touch = Input.GetTouch(i);
                touchPosition1 = Camera.main.ScreenToWorldPoint(touch.position);
                if (area.OverlapPoint(touchPosition1))
                {
                    switch (touch.phase)
                    {
                        case TouchPhase.Began:
                            startPos = touchPosition1;
                            joyBack.transform.position = startPos;
                            joyFront.transform.position = startPos;
                            joyBack.SetActive(true);
                            joyFront.SetActive(true);
                            joyFront.transform.position = startPos;
                            joyBack.transform.position = startPos;
                            Debug.Log("begun");
                            break;

                        case TouchPhase.Moved:
                            direction = touchPosition1 - startPos;
                            Vector2 touchPosition2 = Vector2.ClampMagnitude(direction, 1.75f);
                            horizontal = touchPosition2.x;
                            vertical = touchPosition2.y;
                            //Debug.Log(horizontal);
                            distance = horizontal * horizontal + vertical * vertical;
                            distance = Mathf.Sqrt(distance);
                            direction = direction / distance;
                            direction = Limiters(direction, limit);
                            joyFront.transform.position =new Vector2(startPos.x + touchPosition2.x, startPos.y + touchPosition2.y);
                            Debug.DrawLine(startPos, startPos + touchPosition2);
                            Debug.Log(direction);
                            break;

                        case TouchPhase.Ended:
                            distance = 0;
                            Debug.Log("Ended");
                            joyBack.SetActive(false);
                            joyFront.SetActive(false);
                            break;

                    }
                }
                else
                {
                    joyBack.SetActive(false);
                    joyFront.SetActive(false);
                    distance = 0;
                }

            }
        }
    }
    private static Vector2 Limiters(Vector2 direction, float limit)
    {
        if (direction.x > limit)
        {
            direction.x = limit;
        }
        else if (direction.x < -limit)
        {
            direction.x = -limit;
        }
        if (direction.y > limit)
        {
            direction.y = limit;
        }
        else if (direction.y < -limit)
        {
            direction.y = -limit;
        }
        return direction;
    }
    private void FixedUpdate()
    {
        move.Translate((direction) * movespeed * distance * Time.fixedDeltaTime);

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文