【问题标题】:Rotating a player around the centre with touch controls使用触摸控件围绕中心旋转播放器
【发布时间】:2020-07-30 23:50:17
【问题描述】:

我的播放器确实围绕中心对象旋转:

 private void FixedUpdate()
 {
     transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
 }

这适用于键盘,我需要它来进行触摸控制。就像当我触摸屏幕左侧时玩家应该沿着半径向左移动一样,右侧也是如此。我很难弄清楚触摸的控件。任何帮助,将不胜感激。谢谢。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您的实际问题似乎是

    如何检查用户是触摸屏幕的左半部分还是右半部分?

    您可以通过将touch.position.xScreen.width / 2f 进行比较来轻松完成此操作。如果它小于你触摸左边,否则你触摸右边。

    所以你可能会做类似的事情

    [SerializeField] private float moveSpeed = 45f;
    
    void Update()
    {
        if(Input.touchCount > 0)
        {
            // Check whether the touch is on the left or right side of the screen
            // so basically x is lower or higher than the screen center
            var direction = Input.GetTouch(0).position.x < Screen.width / 2f ? 1 : -1;
    
            // use the direction multiplier for the rotation direction
            transform.RotateAround(Vector3.zero, Vector3.forward, moveSpeed * direction * Time.deltaTime);
        }
    }
    

    为了更容易调试/测试,您实际上可以将它与鼠标输入结合起来,例如

    void Update()
    {
        if(Input.touchSupported)
        {
            if(Input.touchCount > 0)
            {
                var direction = Input.GetTouch(0).position.x < Screen.width / 2f ? 1 : -1;
                transform.RotateAround(Vector3.zero, Vector3.forward, moveSpeed * direction * Time.deltaTime);
            }
        }
        else
        {
            if (Input.GetMouseButton(0))
            {
                var direction = Input.mousePosition.x < Screen.width / 2f ? 1 : -1;
                transform.RotateAround(Vector3.zero, Vector3.forward, moveSpeed * direction * Time.deltaTime);
            }
        }
    }
    

    【讨论】:

    • 非常感谢您,先生!它就像我想要的那样工作! :D
    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    相关资源
    最近更新 更多