繁体   English   中英

使用触摸控件围绕中心旋转播放器

[英]Rotating a player around the centre with touch controls

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

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

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

在此处输入图像描述

看来您的实际问题是

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

您可以通过将touch.position.x与 Screen.width Screen.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);
        }
    }
}

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM