namespace UnityEngine.XR.Interaction.Toolkit.Inputs
{
///
/// One of the four primary directions.
///
///
public enum Cardinal
{
///
/// North direction, e.g. forward on a thumbstick.
///
North,
///
/// South direction, e.g. back on a thumbstick.
///
South,
///
/// East direction, e.g. right on a thumbstick.
///
East,
///
/// West direction, e.g. left on a thumbstick.
///
West,
}
///
/// Utility functions related to directions.
///
public static class CardinalUtility
{
///
/// Get the nearest cardinal direction for a given .
///
/// Input vector, such as from a thumbstick.
/// Returns the nearest direction.
///
/// Arbitrarily biases towards and
/// to disambiguate when angle is exactly equidistant between directions.
///
public static Cardinal GetNearestCardinal(Vector2 value)
{
var angle = Mathf.Atan2(value.y, value.x) * Mathf.Rad2Deg;
var absAngle = Mathf.Abs(angle);
if (absAngle < 45f)
return Cardinal.East;
if (absAngle > 135f)
return Cardinal.West;
return angle >= 0f ? Cardinal.North : Cardinal.South;
}
}
}