【发布时间】:2015-06-30 06:53:12
【问题描述】:
如果玩家触摸,他会跳跃并滑动(左 - 右只是从左到右移动......就像任何跑步者一样)。 一切似乎都很好,但总是有一个 BUT 代码如下:
Vector3 startPos;
float minSwipeDistX, minSwipeDistY;
bool isJump = false;
void Start()
{
minSwipeDistX = minSwipeDistY = Screen.width / 6;
}
bool isSwipe = false;
bool isTouch = false;
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Moved:
isTouch = true;
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0 && !isSwipe)//to right swipe
{
isTouch = false;
isSwipe = true;
Debug.Log("Right");
}
else if (swipeValue < 0 && !isSwipe)//to left swipe
{
isTouch = false;
isSwipe = true;
Debug.Log("Left");
}
}
// add swipe to up
if (swipeDistVertical > minSwipeDistY)
{
float swipeValueY = Mathf.Sign(touch.position.y - startPos.y);
if (swipeValueY > 0 && !isSwipe)
{
isTouch = false;
isSwipe = true;
Debug.Log("Up");
}
}
break;
case TouchPhase.Stationary:
isJump = true;
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
isTouch = false;
isSwipe = false;
break;
}
}
}
void FixedUpdate()
{
if (isJump && !isTouch)
{
Debug.Log("Tap");
isJump = false;
}
}
当进行简单的触摸时 (TouchPhase.Stationary) 会立即对玩家的跳跃做出反应。
当我放开手指时,什么时候跳转向上滑动然后跳转播放器。我通过我使用的 (TouchPhase.Ended) 来理解这一切。
我尝试放置 TouchPhase.Moved 但之后一直在运动之前使用跳转 (TouchPhase.Stationary)。
毛格谁遇到过这样的问题?
如果是这样,那么告诉我如何做到这一点是触摸和滑动滑动触摸。
【问题讨论】:
-
我不得不说我不明白你的问题。请尝试清楚地解释您所期望的行为。
-
如果我点击屏幕 - 跳跃(一次),如果我滑动(向左或向右) - 滑动(一次)。没用过(
TouchPhase.Ended) -
请重新表述您问题的下半部分,您无法从中理解您需要什么。
标签: c# ios unity3d game-physics