尝试检测玩家的触摸是否在游戏区域内,如果是,则启动计时器。我已经启动了一个计时器部分,用于键盘输入,只是没有触摸(移动)输入。
if(...) {
timer.Start();
}
我的游戏板是带有精灵的矩形变换:
修改
我尝试了副本中的内容,但它仍然没有用。
public class PlayerController : MonoBehaviour, IBeginDragHandler
{
void Start()
{
...
// For touch controls
addPhysics2DRaycaster();
}
// For touch controls
void addPhysics2DRaycaster()
{
Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
if (physicsRaycaster == null)
{
Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
}
}
}
这是我尝试启动计时器的地方:
public void OnBeginDrag(PointerEventData eventData)
{
timer.Start();
}
以下是控件的代码:
// FixedUpdate() - called before physics calculations
void FixedUpdate() {
// Touch swipe input
if(controlToggle.isOn) {
joystick.SetActive(false);
controlToggle.GetComponentInChildren<Text>().text = "Swipe";
if(Input.touchCount > 0) {
Touch myTouch = Input.touches[0];
if(myTouch.phase == TouchPhase.Began) {
touchOrigin = myTouch.position;
} else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
Vector2 touchEnd = myTouch.position;
float x = touchEnd.x - touchOrigin.x;
float y = touchEnd.y - touchOrigin.y;
touchOrigin.x = -1;
float moveHorizontal = x;
float moveVertical = y;
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
rb2d.AddForce(movement * speed);
}
}
}
...
}