if you want to allow player to control it with swipes you can use this code :
enter code here
private Vector2 fingerDownPosition;
private Vector2 fingerUpPosition;
[SerializeField]
private bool detectSwipeOnlyAfterRelease = true;
[SerializeField]
private float minDistanceForSwipe = 20f;
public static event Action<SwipeData> OnSwipe = delegate { };
private void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fingerUpPosition = touch.position;
fingerDownPosition = touch.position;
}
if (!detectSwipeOnlyAfterRelease && touch.phase == TouchPhase.Moved)
{
fingerDownPosition = touch.position;
DetectSwipe();
}
if (touch.phase == TouchPhase.Ended)
{
fingerDownPosition = touch.position;
DetectSwipe();
}
}
}
private void DetectSwipe()
{
if (SwipeDistanceCheckMet())
{
if (IsVerticalSwipe())
{
var direction = fingerDownPosition.y - fingerUpPosition.y > 0 ? SwipeDirection.Up : SwipeDirection.Down;
SendSwipe(direction);
}
else
{
var direction = fingerDownPosition.x - fingerUpPosition.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
SendSwipe(direction);
}
fingerUpPosition = fingerDownPosition;
}
}
private bool IsVerticalSwipe()
{
return VerticalMovementDistance() > HorizontalMovementDistance();
}
private bool SwipeDistanceCheckMet()
{
return VerticalMovementDistance() > minDistanceForSwipe || HorizontalMovementDistance() > minDistanceForSwipe;
}
private float VerticalMovementDistance()
{
return Mathf.Abs(fingerDownPosition.y - fingerUpPosition.y);
}
private float HorizontalMovementDistance()
{
return Mathf.Abs(fingerDownPosition.x - fingerUpPosition.x);
}
private void SendSwipe(SwipeDirection direction)
{
SwipeData swipeData = new SwipeData()
{
Direction = direction,
StartPosition = fingerDownPosition,
EndPosition = fingerUpPosition
};
OnSwipe(swipeData);
}}public struct SwipeData{ public Vector2 StartPosition;public Vector2 EndPosition;
public SwipeDirection Direction;
}
public enum SwipeDirection
{
Up,
Down,
Left,
Right
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…