簡體   English   中英

如何將鼠標輸入統一轉換為移動觸摸

[英]How to convert mouse input to mobile touch in unity

我是游戲開發的新手。 我正在努力使自己起來。 不幸的是,我試圖更改當前代碼以使其可用於移動觸摸屏。 您能幫我怎樣制作移動代碼?

private Vector2 mousePos;
private Rigidbody2D rb;

private Vector2 offsetClicked;
private Vector2 offsetReleased;

private void Start () {
    rb = GetComponent<Rigidbody2D> ();
    offsetReleased = transform.position;
}

private void FixedUpdate () {
    mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

    if (Input.GetMouseButton (0)) {
        Vector2 newPos = new Vector2 (
            Mathf.Clamp(mousePos.x + offsetClicked.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),
            mousePos.y + offsetClicked.y
        );

        rb.MovePosition (newPos);
        offsetReleased = newPos - (Vector2) Camera.main.transform.position;
    } //Clicked
    else {
        Vector2 newPos = new Vector2 (
            Mathf.Clamp (Camera.main.transform.position.x + offsetReleased.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),
            Camera.main.transform.position.y + offsetReleased.y
        );

        rb.MovePosition (newPos);
        offsetClicked = newPos - mousePos;
    } //Released
}

上面的代碼適用於鼠標。

而不是檢查是否單擊了鼠標按鈕,而是檢查是否存在任何觸摸,然后提取觸摸位置。 Input.touches返回上一幀的觸摸列表,因此您可以使用該列表查找將替換代碼中的mousePos的觸摸的位置。

Touch myTouch;
Vector2 myTouchPosition;

Update()
{
    if (Input.touchCount > 0)
    {
        myTouch = Input.touches[0]; //Get the first touch
        myTouchPosition = Camera.main.ScreenToWorldPoint(myTouch.position);
    }
}

此外,您可以使用觸摸階段來確定觸摸期間您希望發生什么事情。

CrossPlatformInput包導入到您的項目中,您實際上可以繼續使用Input.GetMouseButton(xxx)。

using UnityEngine;
using CrossPlatformInput;

    public class MyClass : MonoBehaviour {

    private Vector2 mousePos;
    private Rigidbody2D rb;

    private Vector2 offsetClicked;
    private Vector2 offsetReleased;

    private void Start () {
        rb = GetComponent<Rigidbody2D> ();
        offsetReleased = transform.position;
    }

    private void FixedUpdate () {
        mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);

        if (Input.GetMouseButton (0)) {
            Vector2 newPos = new Vector2 (
                Mathf.Clamp(mousePos.x + offsetClicked.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),
                mousePos.y + offsetClicked.y
            );

            rb.MovePosition (newPos);
            offsetReleased = newPos - (Vector2) Camera.main.transform.position;
        } //Clicked
        else {
            Vector2 newPos = new Vector2 (
                Mathf.Clamp (Camera.main.transform.position.x + offsetReleased.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),
                Camera.main.transform.position.y + offsetReleased.y
            );

            rb.MovePosition (newPos);
            offsetClicked = newPos - mousePos;
        } //Released
    }

}

(請參見此處: https : //docs.unity3d.com/Manual/CrossPlatformConsiderations.html

觸碰和點擊

設計Input.GetMouseButtonXXX函數,以使它們在移動設備上具有相當明顯的解釋,即使沒有“鼠標”。 屏幕上的單次觸摸被報告為左鍵單擊,並且Input.mousePosition屬性提供了觸摸的位置,只要手指正在觸摸屏幕即可。 這意味着具有簡單鼠標交互功能的游戲通常可以在台式機和移動平台之間透明地運行。 但是,自然地,轉換通常比這簡單得多。 桌面游戲可以使用多個鼠標按鈕,而手機游戲可以一次檢測屏幕上的多次觸摸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM