在我用于android的c#游戏中,if语句检查玩家是否向左滑动并且最近的对象是某个对象(向左上或向下),然后击中值将等于1,但它们没有按预期工作。某些原因,每当我开始游戏时,攻击值总是= 1,它应该设置为0。 另一个小问题是等待Coroutines在刷了几次后停止工作2秒钟。
所有帮助都非常苛刻
这是c#脚本
using UnityEngine;
using System.Collections;
public class SwipeChecker : MonoBehaviour
{
public float maxTime;
public float minSwipeDistance;
float startTime;
float endTime;
Vector3 startPos;
Vector3 endPos;
float swipeDistance;
float swipeTime;
public float swipeScore;
public GameObject left;
public GameObject right;
public GameObject up;
public GameObject down;
public GameObject swipeChecker;
public GameObject[] platforms = new GameObject[5];
public bool leftSwipe;
public bool rightSwipe;
public bool upSwipe;
public bool downSwipe;
public float strike;
public GameObject closestPlatform;
// Use this for initialization
public GameObject FindClosestPlatform()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag("platform");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
public IEnumerator leftwait()
{
leftSwipe = true;
yield return new WaitForSeconds(2);
leftSwipe = false;
}
public IEnumerator rightwait()
{
rightSwipe = true;
yield return new WaitForSeconds(2);
rightSwipe = false;
}
public IEnumerator upwait()
{
upSwipe = true;
yield return new WaitForSeconds(2);
upSwipe = false;
}
public IEnumerator downwait()
{
downSwipe = true;
yield return new WaitForSeconds(2);
downSwipe = false;
}
public IEnumerator pause()
{
yield return new WaitForSeconds(0.5f);
}
void Start()
{
strike = 0;
}
// Update is called once per frame
void Update()
{
closestPlatform = FindClosestPlatform();
left = GameObject.Find("PurpleTile(Clone)");
right = GameObject.Find("OrangeTile(Clone)");
up = GameObject.Find("BlueTile(Clone)");
down = GameObject.Find("RedTile(Clone)");
if ((closestPlatform == left) && !leftSwipe)
{
strike = 1;
}
if ((closestPlatform == right) && !rightSwipe)
{
strike = 1;
}
if ((closestPlatform == up) && !upSwipe)
{
strike = 1;
}
if ((closestPlatform == down) && !downSwipe)
{
strike = 1;
}
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startTime = Time.time;
startPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
endTime = Time.time;
endPos = touch.position;
swipeDistance = (endPos - startPos).magnitude;
swipeTime = endTime - startTime;
if (swipeTime < maxTime && swipeDistance > minSwipeDistance)
{
swipe();
}
}
}
}
void swipe()
{
Vector2 distance = endPos - startPos;
if (Mathf.Abs(distance.x) > Mathf.Abs(distance.y))
{
Debug.Log("Horizontal Swipe");
if (distance.x > 0)
{
Debug.Log("Right Swipe");
rightSwipe = true;
StartCoroutine(rightwait());
}
if (distance.x < 0)
{
Debug.Log("Left Swipe");
StartCoroutine(leftwait());
}
}
else if (Mathf.Abs(distance.x) < Mathf.Abs(distance.y))
{
Debug.Log("Vertical Swipe");
if (distance.y > 0)
{
Debug.Log("Up Swipe");
StartCoroutine(upwait());
}
if (distance.y < 0)
{
Debug.Log("Down Swipe");
StartCoroutine(downwait());
}
}
}
}