Hallo dieses Skript nicht getestet, es hilft schrieb.
Edit: ich nicht gesucht habe, bevor diese Antwort veröffentlichen, aber ich denke this answer Besser ist
public class GestureRecognizer: MonoBehaviour {
// Min length to detect the Swipe
public float MinSwipeLength = 5f;
private Vector2 _firstPressPos;
private Vector2 _secondPressPos;
private Vector2 _currentSwipe;
private Vector2 _firstClickPos;
private Vector2 _secondClickPos;
public enum Swipe {
None,
Up,
Down,
Left,
Right,
UpLeft,
UpRight,
DownLeft,
DownRight
};
public static Swipe SwipeDirection;
public float ReturnForce = 10f;
private void Update() {
DetectSwipe();
}
public void DetectSwipe() {
if (Input.touches.Length > 0) {
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began) {
_firstPressPos = new Vector2(t.position.x, t.position.y);
}
if (t.phase == TouchPhase.Ended) {
_secondPressPos = new Vector2(t.position.x, t.position.y);
_currentSwipe = new Vector3(_secondPressPos.x - _firstPressPos.x, _secondPressPos.y - _firstPressPos.y);
// Make sure it was a legit swipe, not a tap
if (_currentSwipe.magnitude < MinSwipeLength) {
SwipeDirection = Swipe.None;
return;
}
_currentSwipe.Normalize();
// Swipe up
if (_currentSwipe.y > 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) {
SwipeDirection = Swipe.Up;
}
// Swipe down
else if (_currentSwipe.y < 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) {
SwipeDirection = Swipe.Down;
}
// Swipe left
else if (_currentSwipe.x < 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) {
SwipeDirection = Swipe.Left;
}
// Swipe right
else if (_currentSwipe.x > 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) {
SwipeDirection = Swipe.Right;
}
// Swipe up left
else if (_currentSwipe.y > 0 && _currentSwipe.x < 0) {
SwipeDirection = Swipe.UpLeft;
}
// Swipe up right
else if (_currentSwipe.y > 0 && _currentSwipe.x > 0) {
SwipeDirection = Swipe.UpRight;
}
// Swipe down left
else if (_currentSwipe.y < 0 && _currentSwipe.x < 0) {
SwipeDirection = Swipe.DownLeft;
// Swipe down right
} else if (_currentSwipe.y < 0 && _currentSwipe.x > 0) {
SwipeDirection = Swipe.DownRight;
}
}
} else {
if (Input.GetMouseButtonDown(0)) {
_firstClickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
} else {
SwipeDirection = Swipe.None;
//Debug.Log ("None");
}
if (Input.GetMouseButtonUp(0)) {
_secondClickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
_currentSwipe = new Vector3(_secondClickPos.x - _firstClickPos.x, _secondClickPos.y - _firstClickPos.y);
// Make sure it was a legit swipe, not a tap
if (_currentSwipe.magnitude < MinSwipeLength) {
SwipeDirection = Swipe.None;
return;
}
_currentSwipe.Normalize();
//Swipe directional check
// Swipe up
if (_currentSwipe.y > 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) {
SwipeDirection = Swipe.Up;
Debug.Log("Up");
}
// Swipe down
else if (_currentSwipe.y < 0 && _currentSwipe.x > -0.5f && _currentSwipe.x < 0.5f) {
SwipeDirection = Swipe.Down;
Debug.Log("Down");
}
// Swipe left
else if (_currentSwipe.x < 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) {
SwipeDirection = Swipe.Left;
Debug.Log("Left");
}
// Swipe right
else if (_currentSwipe.x > 0 && _currentSwipe.y > -0.5f && _currentSwipe.y < 0.5f) {
SwipeDirection = Swipe.Right;
Debug.Log("right");
} // Swipe up left
else if (_currentSwipe.y > 0 && _currentSwipe.x < 0) {
SwipeDirection = Swipe.UpLeft;
Debug.Log("UpLeft");
}
// Swipe up right
else if (_currentSwipe.y > 0 && _currentSwipe.x > 0) {
SwipeDirection = Swipe.UpRight;
Debug.Log("UpRight");
}
// Swipe down left
else if (_currentSwipe.y < 0 && _currentSwipe.x < 0) {
SwipeDirection = Swipe.DownLeft;
Debug.Log("DownLeft");
// Swipe down right
} else if (_currentSwipe.y < 0 && _currentSwipe.x > 0) {
SwipeDirection = Swipe.DownRight;
Debug.Log("DownRight");
}
}
}
}
Mögliche Duplikat [Detect Swipe-Geste Richtung] (http: // stackoverflow.com/questions/41491765/detect-swipe-gesture-direction) – Isuka
Auch Sie [bereits gefragt] (http://stackoverflow.com/questions/44049130/how-i-can-detect-swipe-on-unity3d) diese Frage gestern, und es wurde aus dem gleichen Grund geschlossen. Versuchen Sie, die andere Frage zu überprüfen, es gibt viele nützliche Informationen, um Sie zu beginnen. – Isuka
Ich muss erkennen, LeftUP und Links unten/rechts oben und rechts unten/unten (von links nach rechts)/unten (von rechts nach links) und oben (von rechts nach links)/oben (von links nach rechts) können Sie dieses Spiel sehen [Link ] (https://itunes.apple.com/us/app/fidget-hand-spinner/id1225134960?mt=8) - – Abs3akt