簡體   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