如何统一将鼠标输入转换为移动触摸

我是游戏开发的新手。我正在尝试做一些像上升一样的东西。我试图更改流动代码以使其可用于移动触摸屏,但不幸的是失败了。你能帮我如何为移动设备制作流动代码吗?


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

}

以上代码适用于鼠标。


HUX布斯
浏览 272回答 2
2回答

红糖糍粑

将CrossPlatformInput包导入到您的项目中,您实际上可以继续使用 Input.GetMouseButton(xxx)。using UnityEngine;using CrossPlatformInput;&nbsp; &nbsp; public class MyClass : MonoBehaviour {&nbsp; &nbsp; private Vector2 mousePos;&nbsp; &nbsp; private Rigidbody2D rb;&nbsp; &nbsp; private Vector2 offsetClicked;&nbsp; &nbsp; private Vector2 offsetReleased;&nbsp; &nbsp; private void Start () {&nbsp; &nbsp; &nbsp; &nbsp; rb = GetComponent<Rigidbody2D> ();&nbsp; &nbsp; &nbsp; &nbsp; offsetReleased = transform.position;&nbsp; &nbsp; }&nbsp; &nbsp; private void FixedUpdate () {&nbsp; &nbsp; &nbsp; &nbsp; mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);&nbsp; &nbsp; &nbsp; &nbsp; if (Input.GetMouseButton (0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vector2 newPos = new Vector2 (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mathf.Clamp(mousePos.x + offsetClicked.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mousePos.y + offsetClicked.y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rb.MovePosition (newPos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetReleased = newPos - (Vector2) Camera.main.transform.position;&nbsp; &nbsp; &nbsp; &nbsp; } //Clicked&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vector2 newPos = new Vector2 (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mathf.Clamp (Camera.main.transform.position.x + offsetReleased.x, GameManager.gm.cameraEdges.w + 0.32f, GameManager.gm.cameraEdges.y - 0.32f),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Camera.main.transform.position.y + offsetReleased.y&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rb.MovePosition (newPos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; offsetClicked = newPos - mousePos;&nbsp; &nbsp; &nbsp; &nbsp; } //Released&nbsp; &nbsp; }}(见这里:https : //docs.unity3d.com/Manual/CrossPlatformConsiderations.html)触摸和点击Input.GetMouseButtonXXX 函数的设计使得它们在移动设备上具有相当明显的解释,即使没有“鼠标”本身。屏幕上的单次触摸报告为左键单击,只要手指触摸屏幕,Input.mousePosition 属性就会给出触摸的位置。这意味着具有简单鼠标交互的游戏通常可以在桌面和移动平台之间透明地运行。当然,转换通常比这简单得多。桌面游戏可以使用多个鼠标按钮,而手机游戏可以一次检测屏幕上的多次触摸。

绝地无双

不是检查是否单击了鼠标按钮,而是检查是否存在任何触摸,然后提取触摸位置。Input.touches返回最后一帧期间的触摸列表,因此您可以使用它来查找将mousePos在代码中替换的触摸位置。Touch myTouch;Vector2 myTouchPosition;Update(){&nbsp; &nbsp; if (Input.touchCount > 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; myTouch = Input.touches[0]; //Get the first touch&nbsp; &nbsp; &nbsp; &nbsp; myTouchPosition = Camera.main.ScreenToWorldPoint(myTouch.position);&nbsp; &nbsp; }}此外,您可以使用触摸阶段来确定您希望在触摸期间的哪个时间点发生某些事情。
打开App,查看更多内容
随时随地看视频慕课网APP