Unity C# 点击并滑动 android
Unity C# tap and swipe on android
我目前正在编写一个与障碍物统一的简单游戏,这是一个无尽的游戏,玩家可以跳跃,向右和向左移动以避开这些障碍物。该游戏适用于华硕和华为设备,但不适用于三星。使用三星设备,当我点击屏幕时播放器不会跳转,但滑动有效。
我的代码:
void change()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Ended:
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)
{//right swipe
anim.Play(change_Line_Animation);
transform.localPosition = second_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
else if (swipeValue < 0)
{//left swipe
anim.Play(change_Line_Animation);
transform.localPosition = first_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
}
else {
if (!player_Jumped){
anim.Play(jump_Animation);
player_Jumped = true;
SoundManager.instance.PlayJumpSound();
}
}
break;
}
}
}
这个函数在update()函数中调用。
谢谢
由于我是新来的,我还不能使用评论来提出更多问题。
使用三星时究竟是什么不起作用?
虽然你可以简化计算。
float swipeValue = touch.position.x - startPos.x; //Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)
{//right swipe
anim.Play(change_Line_Animation);
transform.localPosition = second_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
else if (swipeValue < 0)
{//left swipe
anim.Play(change_Line_Animation);
transform.localPosition = first_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
仅与 > 或 < 比 0 比较时不需要 Math.Sign。
case TouchPhase.Began:
startPos = touch.position.x; // startPos could be float
break;
因此,
float swipeDistHorizontal = Mathf.Abs(touch.position.x - startPos);
如果游戏只使用水平滑动,则不需要向量来存储和计算滑动增量。
如果您提供更多信息,我很乐意提供更多帮助。
我目前正在编写一个与障碍物统一的简单游戏,这是一个无尽的游戏,玩家可以跳跃,向右和向左移动以避开这些障碍物。该游戏适用于华硕和华为设备,但不适用于三星。使用三星设备,当我点击屏幕时播放器不会跳转,但滑动有效。
我的代码:
void change()
{
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Ended:
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)
{//right swipe
anim.Play(change_Line_Animation);
transform.localPosition = second_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
else if (swipeValue < 0)
{//left swipe
anim.Play(change_Line_Animation);
transform.localPosition = first_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
}
else {
if (!player_Jumped){
anim.Play(jump_Animation);
player_Jumped = true;
SoundManager.instance.PlayJumpSound();
}
}
break;
}
}
}
这个函数在update()函数中调用。
谢谢
由于我是新来的,我还不能使用评论来提出更多问题。 使用三星时究竟是什么不起作用?
虽然你可以简化计算。
float swipeValue = touch.position.x - startPos.x; //Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)
{//right swipe
anim.Play(change_Line_Animation);
transform.localPosition = second_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
else if (swipeValue < 0)
{//left swipe
anim.Play(change_Line_Animation);
transform.localPosition = first_PosOfPlayer;
SoundManager.instance.PlayMoveLineSound();
}
仅与 > 或 < 比 0 比较时不需要 Math.Sign。
case TouchPhase.Began:
startPos = touch.position.x; // startPos could be float
break;
因此,
float swipeDistHorizontal = Mathf.Abs(touch.position.x - startPos);
如果游戏只使用水平滑动,则不需要向量来存储和计算滑动增量。
如果您提供更多信息,我很乐意提供更多帮助。