[英]Unity3d Touch Swipe
如果玩家觸摸,他將跳躍並滑動(從左到右,就像任何跑步者一樣,從左到右移動)。 一切似乎都不錯,但是一如既往,但是這里有代碼:
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
)。 莫格誰面對過這樣的問題? 如果是這樣,那么告訴我該怎么做,就是輕觸並輕拂輕觸。
這是我的代碼:
void Update()
{
#if UNITY_ANDROID || UNITY_IPHONE
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
StartCoroutine(Jump());
break;
case TouchPhase.Moved:
isSwipe = 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 && !isTouch)//to right swipe
{
isTouch = true;
StartCoroutine(Right());
}
else if (swipeValue < 0 && !isTouch)//to left swipe
{
isTouch = true;
StartCoroutine(Left());
}
}
//add swipe to up
if(swipeDistVertical > minSwipeDistY)
{
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
if(swipeValue > 0 && !isTouch)
{
isTouch = true;
StartCoroutine(Jump2());
}
}
break;
case TouchPhase.Ended:
isSwipe = false;
isTouch = false;
break;
}
}
#endif
}
IEnumerator Jump2()
{
yield return new WaitForSeconds(0.05f);
if(playerVelocity <= 0.2f)
{
Debug.Log("Swipe Up");
}
}
IEnumerator Jump()
{
if (!isSwipe)
{
yield return new WaitForSeconds(0.05f);
if (!isSwipe && playerVelocity <= 0.2f)
{
Debug.Log("Tap");
}
else
{
yield break;
}
}
else
{
yield break;
}
}
IEnumerator Right()
{
Debug.Log("Right");
}
IEnumerator Left()
{
Debug.Log("Left");
}
我想到了:
它應該把你帶到正確的方向
float validInputThresold = 5f;
enum Gestures {
None,
Stationary,
SwipeRight,
SwipeLeft,
SwipeUp,
SwipeDown
}
Gestures currentGesture;
public void Update() {
currentGesture = Gestures.None;
HandleTouch(Input.GetTouch(0));
HandleCharacterMovement(currentGesture);
}
Vector3 originalPosition;
void HandleTouch(Touch touch) {
if (touch == null) return;
switch (touch.phase) {
case TouchPhase.Began:
originalPosition = touch.position;
break;
case TouchPhase.Moved:
Vector3 delta = touch.position - originalPosition;
if (delta.magnitude > validInputThresold) Moved(touch, delta);
break;
case TouchPhase.Stationary:
currentGesture = Gestures.Stationary;
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
currentGesture = Gestures.None;
break;
}
}
public float gestureThresold = 10f;
void Moved(Touch touch, Vector3 delta) {
if ((Mathf.Abs(delta.x)<=gestureThresold && Mathf.Abs(delta.y)<=gestureThresold)
|| (Mathf.Abs(delta.x)>gestureThresold && Mathf.Abs(delta.y)>gestureThresold) )
currentGesture = Gestures.Stationary; //IF FINGER MOVED IN DIAGONAL, INVALID STATE FALLSBACK TO STATIONARY
else if (delta.x > gestureThresold) currentGesture = Gestures.SwipeRight;
else if (delta.x < -gestureThresold) currentGesture = Gestures.SwipeLeft;
else if (delta.y > gestureThresold) currentGesture = Gestures.SwipeUp;
else if (delta.y < -gestureThresold) currentGesture = Gestures.SwipeDown;
}
bool playerIsMoving = false;
// ALL THE ROUTINES HERE SET THE PLAYER IS MOVING FLAG
// TO TRUE AND THEN TO FALSE WHEN THE MOVEMENT IS DONE
void HandleCharacterMovement(Gestures gesture) {
if (playerIsMoving) return;
switch (gesture) {
default:
case Gestures.None:
case Gestures.Stationary:
return;
case Gestures.SwipeRight:
StartCoroutine(MovePlayerRight());
return;
case Gestures.SwipeLeft:
StartCoroutine(MovePlayerLeft());
return;
case Gestures.SwipeUp:
StartCoroutine(Jump());
return;
case Gestures.SwipeDown:
StartCoroutine(Crouch());
return;
}
}
不確定是否有效,請親自進行。 隨時糾正它。
其背后的想法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.