Skip to content

Instantly share code, notes, and snippets.

Created March 29, 2017 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/23d4782f0d4ac5867d6994aee4df8288 to your computer and use it in GitHub Desktop.
Save anonymous/23d4782f0d4ac5867d6994aee4df8288 to your computer and use it in GitHub Desktop.
Swipe on direction
public enum Swipes { None, Up, Down, Left, TopLeft, BottomLeft, Right, TopRight, BottomRight };
/// <summary>
/// Class attached to the GameObject "SwipeDetector" in the scene hierarchy.
/// In charge to handle swipe on the screen.
/// </summary>
///
public class Swipe : MonoBehaviour
{
public int force;
public float minSwipeLength = 200f;
Vector2 currentSwipe;
private Vector2 fingerStart;
private Vector2 fingerEnd;
private bool _swiped = false;
public Gameplay GamePlay;
public static Swipes direction;
/// <summary>
/// Delegate triggered when swipe event is detected.
/// </summary>
public delegate void OnSwipe(Swipes direction);
public static event OnSwipe OnSwipeEnd;
public static void ForceOnSwipeEnd(Swipes direction)
{
direction = Swipes.None;
if (OnSwipeEnd != null)
OnSwipeEnd(direction);
}
void Awake()
{
GamePlay = GameObject.Find("Platforms").GetComponent<Gameplay>();
var screenV = new Vector2(Screen.width, Screen.height);
print("screen magnitude = " + screenV.magnitude);
OnSwipeEnd = delegate(Swipes swipes)
{
switch (swipes)
{
case Swipes.Left:
GamePlay.Attack(Direction.Left);
break;
case Swipes.TopLeft:
GamePlay.Attack(Direction.Left);
break;
case Swipes.Up:
GamePlay.Attack(Direction.Top);
break;
case Swipes.TopRight:
GamePlay.Attack(Direction.Top);
break;
case Swipes.Right:
GamePlay.Attack(Direction.Right);
break;
case Swipes.BottomRight:
GamePlay.Attack(Direction.Right);
break;
case Swipes.Down:
GamePlay.Attack(Direction.Down);
break;
case Swipes.BottomLeft:
GamePlay.Attack(Direction.Down);
break;
}
};
minSwipeLength = screenV.magnitude * 0.10f;
}
void Update()
{
if (OnSwipeEnd == null)
{
fingerStart = fingerEnd;
return;
}
var invokList = OnSwipeEnd.GetInvocationList();
var l = invokList.Length;
if (l <= 0)
{
direction = Swipes.None;
fingerStart = Vector2.zero;
fingerEnd = Vector2.zero;
return;
}
SwipeDetection();
}
/// <summary>
/// Swipe detection logic
/// </summary>
public void SwipeDetection()
{
if (Input.GetMouseButtonDown(0))
{
_swiped = false;
fingerStart = Input.mousePosition;
fingerEnd = Input.mousePosition;
}
if(_swiped) return;
if (Input.GetMouseButton(0))
{
fingerEnd = Input.mousePosition;
currentSwipe = new Vector2(fingerEnd.x - fingerStart.x, fingerEnd.y - fingerStart.y);
// Make sure it was a legit swipe, not a tap
if (currentSwipe.magnitude < minSwipeLength)
{
direction = Swipes.None;
if (OnSwipeEnd != null)
OnSwipeEnd(direction);
return;
}
float angle = (Mathf.Atan2(currentSwipe.y, currentSwipe.x) / (Mathf.PI));
if (angle > 0.375f && angle < 0.625f)
{
direction = Swipes.Up;
}
else if (angle < -0.375f && angle > -0.625f)
{
direction = Swipes.Down;
}
else if (angle < -0.875f || angle > 0.875f)
{
direction = Swipes.Left;
}
else if (angle > -0.125f && angle < 0.125f)
{
direction = Swipes.Right;
}
else if (angle > 0.125f && angle < 0.375f)
{
direction = Swipes.TopRight;
}
else if (angle > 0.625f && angle < 0.875f)
{
direction = Swipes.TopLeft;
}
else if (angle < -0.125f && angle > -0.375f)
{
direction = Swipes.BottomRight;
}
else if (angle < -0.625f && angle > -0.875f)
{
direction = Swipes.BottomLeft;
}
if (OnSwipeEnd != null)
{
_swiped = true;
OnSwipeEnd(direction);
}
}
if (Input.GetMouseButtonUp(0))
{
direction = Swipes.None;
if (OnSwipeEnd != null)
OnSwipeEnd(direction);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment