Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created March 6, 2024 02:02
Show Gist options
  • Save baba-s/d88eb1aed831c65586ba9ad80826bd5e to your computer and use it in GitHub Desktop.
Save baba-s/d88eb1aed831c65586ba9ad80826bd5e to your computer and use it in GitHub Desktop.
using UnityEngine;
public class Example : MonoBehaviour
{
private Vector3 m_prevLocalPosition;
private enum DirectionType
{
NONE,
UP,
DOWN,
LEFT,
RIGHT
}
private void Awake()
{
m_prevLocalPosition = transform.localPosition;
}
private void LateUpdate()
{
var movement = transform.localPosition - m_prevLocalPosition;
var directionType =
0.1f < movement.magnitude
? Mathf.Abs( movement.y ) < Mathf.Abs( movement.x )
? 0 < movement.x
? DirectionType.RIGHT
: DirectionType.LEFT
: 0 < movement.y
? DirectionType.UP
: DirectionType.DOWN
: DirectionType.NONE
;
m_prevLocalPosition = transform.localPosition;
Debug.Log( directionType );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment