드레그한 방향 알아내기
두 백터(Vector3) 사이에 임계치(threshold)를 넘어간 거리를 이동한 경우 방향(8방향 기준)을 알아내는 함수가 필요해서 제작해봄.
private enum DIRECTION {LEFT_DOWN = 1, DOWN, RIGHT_DOWN, LEFT, CENTER, RIGHT, LEFT_UP, UP, RIGHT_UP};
DIRECTION GetDirection8Way(Vector3 begin, Vector3 end, int threshold) {
double length = System.Math.Sqrt(System.Math.Pow(end.x - begin.x, 2) + System.Math.Pow(end.y - begin.y, 2));
if (length < threshold) return DIRECTION.CENTER;
double arc = System.Math.Round(System.Math.Atan2(end.y - begin.y, end.x - begin.x) * 180 / System.Math.PI);
if (arc < 0) arc = arc + 360;
if (((0 <= arc) && (22 > arc)) || ((337 <= arc) && (360 > arc))) {
return DIRECTION.RIGHT;
} else if ((22 <= arc) && (67 > arc)) {
return DIRECTION.RIGHT_UP;
} else if ((67 <= arc) && (112 > arc)) {
return DIRECTION.UP;
} else if ((112 <= arc) && (157 > arc)) {
return DIRECTION.LEFT_UP;
} else if ((157 <= arc) && (202 > arc)) {
return DIRECTION.LEFT;
} else if ((202 <= arc) && (247 > arc)) {
return DIRECTION.LEFT_DOWN;
} else if ((247 <= arc) && (292 > arc)) {
return DIRECTION.DOWN;
} else if ((292 <= arc) && (337 > arc)) {
return DIRECTION.RIGHT_DOWN;
} else {
return DIRECTION.CENTER;
}
}
'Unity' 카테고리의 다른 글
[Struct] Point (0) | 2015.11.07 |
---|---|
[Struct] Size (0) | 2015.11.07 |
[Shader] 텍스처 회전 (Texture Rotation) (0) | 2015.09.08 |
C# 리스트(List)를 이용해 만든 우선순위큐(PriorityQueue) (0) | 2015.07.23 |
리플렉션 응용 (C# Reflection) (0) | 2015.07.01 |
MonoBehaviour Lifecycle (0) | 2014.06.30 |
Android Local Notification Plugin (0) | 2013.04.23 |
포물선 공식 (0) | 2013.01.21 |
케릭터 점프 코드 (0) | 2013.01.15 |
Generic Based Singleton for MonoBehaviours (0) | 2013.01.14 |