Замена кнопок на свайп Unity 2D
есть такой вот кусок кода:
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) {
this.movement.SetDirection(Vector2.up);
}
else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) {
this.movement.SetDirection(Vector2.down);
}
else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {
this.movement.SetDirection(Vector2.left);
}
else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) {
this.movement.SetDirection(Vector2.right);
}
Вопрос, как заменить эти кнопки на свайпы
Ответы (2 шт):
Автор решения: Леонид Рябухин
→ Ссылка
Готово:
using UnityEngine;
using UnityEngine.UI;
public class Pacman : MonoBehaviour
{
private bool tap;
private bool isDraging = false;
private Vector2 startTouch, swipeDelta;
private void Update()
{
tap = false;
#region Standalone Inputs
if (Input.GetMouseButtonDown(0))
{
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0)) {
isDraging = false;
Reset();
}
#endregion
#region Mobile Inputs
if (Input.touches.Length > 0) {
if (Input.touches[0].phase == TouchPhase.Began) {
tap = true;
isDraging = true;
startTouch = Input.touches[0].position;
} else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled){
isDraging = false;
Reset();
}
}
#endregion
swipeDelta = Vector2.zero;
if (isDraging) {
if (Input.touches.Length > 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
if (swipeDelta.magnitude > 125) {
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y))
{
if (x < 0)
this.movement.SetDirection(Vector2.left);
else
this.movement.SetDirection(Vector2.right);
}
else {
if (y < 0)
this.movement.SetDirection(Vector2.down);
else
this.movement.SetDirection(Vector2.up);
}
Reset();
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool swipeLeft { get { return swipeLeft; } }
public bool swipeRight { get { return swipeRight; } }
public bool swipeUp { get { return swipeUp; } }
public bool swipeDown { get { return swipeDown; } }
}
это скрипт для свайпа, пользуйтесь на здоровье :)
Автор решения: Andrew
→ Ссылка
можно пользоваться поиском на сайте - он часто помогает.
код свайп детектора взял отсюда: https://ru.stackoverflow.com/questions/937884/Как-отловить-ивент-свайпа-swipe-в-unity/937885#937885
могут быть некоторые мелкие синтаксические ошибки т.к. нету возможности проверить код полноценно.
SwipeDetector swipeDetector = new SwipeDetector();
void start()
{
swipeDetector.OnSwipe += SwipeLeftReaction;
swipeDetector.OnSwipe += SwipeRightReaction;
}
public void SwipeLeftReaction(SwipeData data)
{
if (data.direction == .Left)
Debug.Log("swipe Left");
}
public void SwipeRightReaction(SwipeData data)
{
if (data.direction == .Right)
Debug.Log("swipe Right");
}