gpt4 book ai didi

c# - Unity3d 触摸滑动

转载 作者:行者123 更新时间:2023-11-29 01:53:36 24 4
gpt4 key购买 nike

如果玩家触摸,他会跳跃并滑动(左 - 右只是从左向右移动......就像任何运行者一样)。一切似乎都很好,但一如既往有一个但是这是代码:

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) 会立即对玩家跳跃使用react。当我放开手指时,什么时候跳向上滑动然后跳玩家。我通过使用 (TouchPhase.Ended) 了解这一切。我尝试放置 TouchPhase.Moved,但一直在移动之前使用跳跃 (TouchPhase.Stationary)。Mauger 谁遇到过这样的问题?如果是这样,请告诉我如何做到这一点是触摸和滑动滑动触摸。

最佳答案

这是我的代码:

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");

}

关于c# - Unity3d 触摸滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31131525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com