简单的触摸控制

我一直在我的电脑上测试我的游戏,使用左右箭头,但我想把它切换到触摸控制。


我查看了一些教程,但有人说我需要从商店购买资产,否则它并不真正符合我的需求。


我只需要一些关于我应该采取的最佳方向的指导:)


这是我现在的控件代码,我想知道是否可以使用 2 个按钮,因为我只需要左右移动?


 void Update()

 {

     transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);


     if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < maxWidth)

     {

         targetPos = new Vector2(transform.position.x + Xincrement, transform.position.y);

         transform.position = targetPos;

     }

     else if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x > minWidth)

     {

         targetPos = new Vector2(transform.position.x - Xincrement, transform.position.y);

         transform.position = targetPos;

     }

 }


慕尼黑5688855
浏览 180回答 2
2回答

芜湖不芜

触摸左右半屏移动的代码&nbsp;void Update () {&nbsp;&nbsp; &nbsp; &nbsp;transform.position = Vector2.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;// Detect touch event&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;Touch touch = Input.GetTouch(0);&nbsp; &nbsp; &nbsp;if (touch.phase == TouchPhase.Began)&nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; if (touch.position.x > (Screen.width / 2))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (transform.position.x < maxWidth)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targetPos = new Vector2(transform.position.x + Xincrement, transform.position.y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transform.position = targetPos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (transform.position.x > minWidth)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; targetPos = new Vector2(transform.position.x - Xincrement, transform.position.y);&nbsp; &nbsp; &nbsp;transform.position = targetPos;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;}&nbsp;}

慕的地8271018

使用点击时获取鼠标位置的代码,并检查它是在屏幕的右侧还是左侧
打开App,查看更多内容
随时随地看视频慕课网APP